Two views in a Django page

旧城冷巷雨未停 提交于 2019-12-11 05:53:00

问题


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.

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')

My template

{% 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.

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.

Thanks in advance :)

Update:

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))

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.


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/12373033/two-views-in-a-django-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!