简体   繁体   中英

Why won't my view load in properly? (Django)

I am extremely new to Django's framework and recently finished the introductory series on their website, and I have tried adding a new view in which I use a form to create an object.

The problem is, whenever I access the view, it immediately executes the HttpResponseRedirect(), and second of all it does not even return a response.

views.py

def create(request):
    context = {
        'questionfields': Question.__dict__,
    }
    submitbutton = request.POST.get('Create', False)
    if submitbutton:
        new_question = Question(question_text=request.POST.get('question_text', ''), pub_date=timezone.now())
        if new_question.question_text == '':
            context = {
                'questionfields': Question.__dict__,
                'error_message': "No poll question entered."
            }
            del new_question
            return render(request, 'polls/create.html', context)
        else:
            return HttpResponseRedirect(reverse('create'))

create.html

{% extends "polls/base.html" %}

{% block title %}Create a Poll{% endblock title %}
{% block header %}Create:{% endblock header %}

{% load custom_tags %}

{% block content %}
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'polls:create' %}" method="post"> {% csrf_token %}

   {% for field in questionfields %}
   {% if field == 'question_text'  %}
   <label for="{{ field }}">{{ field|capfirst|replace }}:</label>
   <input type="text" name="{{ field }}" id="{{ field }}">
   <br>
   {% else %}
   {% endif %}
   {% endfor %}
   <br>
   <input type="submit" value="Create" name="submit">

</form>
{% endblock content %}

I attempted to make it so if I enter text into the question_text input, when I click the submit button it creates a Question object with question_text being the entered text and the pub_date being the present time.

However, it merely gives a failed redirect.

I don't fully understand how the render() function works in a view and how its positioning in the logic affects the rendering of the view, so please excuse any of my mistakes.

I would like to know why it does not carry out any of the code from submitbutton... to else: , and how to fix this to get the view to work as intended. If anyone could help with my rendering and view problem it would be great.

Error image

You're using recursive here

return HttpResponseRedirect(reverse('create'))

In case it fails, it will redirect to this Create function again, and fail, and redirect again, again....

Try to do it like this:

def create(request):
    context = {
        'questionfields': Question.__dict__,
    }
    submitbutton = request.POST.get('Create', False)
    if submitbutton:
        new_question = Question(question_text=request.POST.get('question_text', ''), pub_date=timezone.now())
        if new_question.question_text == '':
            context = {
                'questionfields': Question.__dict__,
                'error_message': "No poll question entered."
            }
            del new_question
    # Just render the page here with the initial context
    return render(request, 'polls/create.html', context)
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM