Django页面中有两个视图 [英] Two views in a Django page

查看:111
本文介绍了Django页面中有两个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示页面的输入框时遇到问题。在同一页面上,我想有两个视图,一个访问和检索数据,另一个是窗体。由于它是一个事件注册页面,第一个视图显示了一个特定事件的细节,第二个是从事件本身删除特定用户的视图。

I'm having problems displaying the input box of my page. On this same page I would like to have two views, one that accesses and retrieves data and the other a form. As it's an event sign-up page, the first view show the details of a specific event and the second is a view to remove a specific user from the event itself.

我的views.py

My views.py

def ShowSpecificEvent(request, eventslug):
if request.method == 'POST':
    form = RemovalForm(request.POST)
    if form.is_valid():
        event   = Event.objects.get(slug=eventslug)
        context = {'event': event,}
        updated_event = event.signed_up.remove(for_removal = form.cleaned_data['for_removal'],)
        updated_event.save()
    return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
event   = Event.objects.get(slug=eventslug)
context = {'event': event,}
return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))

def Remove(request, eventslug):
    if request.method == 'POST':
        form = RemovalForm(request.POST)
        if form.is_valid():
            event   = Event.objects.get(slug=eventslug)
            context = {'event': event,}
            updated_event = event.signed_up.remove(for_removal = form.cleaned_data['for_removal'],)
            updated_event.save()
        return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
    return HttpResponseRedirect('base_remove_user.html')

我的模板

{% block content %}

event details...

{% if user.is_authenticated %}
{% if event.sign_up_is_live %}
    <p><a href= "{% url eventattend event.slug %}"> Sign me up!</a></p>

<form action='' method='post'>
{% csrf_token %}
{% if form.errors %}<p>Please correct the following fields:</p>{% endif %}
<div class='register_div'>
{% if form.for_removal.errors %} <p class='error'>{{ form.for_removal.errors }}</p>{% endif %}
<p><label for='for_removal'{% if form.for_removal.errors %}class='error'{% endif %}>User to be removed:</label></p>
<p>{{ form.for_removal }}</p>
</div>  

<p><input type='submit'></p>

</form>

{% endif %}{% endif %}

{% endblock %}

我已阅读我认为是最相关的,但规定的方法并没有真正的帮助。

I've read this which I think is the most relevant but the method prescribed doesn't really help.

我也想到分开这两个页面,但我不会在我的删除用户页面中检索eventslug。

I've also thought of splitting up the two pages, but I won't be able to retrieve the eventslug in my remove user page.

提前感谢:)

更新:

gutrt的答案是一个很大的帮助,我设法通过将ShowSpecificEvent更改为以下内容来显示输入框:

gutrt's answer was a great help and I've managed to get the input box to appear by changing ShowSpecificEvent to the following:

def ShowSpecificEvent(request, eventslug):
event   = Event.objects.get(slug=eventslug)
form    = RemovalForm(request.POST or None)
context = {'event': event, 'form': form,}
if request.method == 'POST':
    if form.is_valid():
        updated_event = event.signed_up.remove(request.POST.get('for_removal'))
        updated_event.save()
        return HttpResponseRedirect('base_user_removed.html')
    else:   
        return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))

但是,提交表单之后,我得到一个ValueError(int()的无效文字,基数为10:'a_technicolor_skye'),其中a_technicolor_skye是我要删除的用户。有人有什么想法吗? Btw,event.signed_up是一个很多的字段,我设置为null = True,blank = True。

However, after submitting the form, I get a ValueError(invalid literal for int() with base 10: 'a_technicolor_skye') where a_technicolor_skye is the user I'm trying to remove. Does anyone have any ideas? Btw, event.signed_up is a many to many field and I've set it to null=True and blank=True.

推荐答案

您可以将所有信息传递到上下文变量中:

You can pass all of the information into the context variable:

context = {'event': event, 'form': my_form}

然后两者都可以在你的模板中使用。请注意,这意味着您将最终使用1 视图,而是计算该视图或其方法中页面的所有信息。

Then both are available in your template. Note that this means you are going to end up using 1 view but computing all the information for the page in that view or its methods.

这篇关于Django页面中有两个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆