简体   繁体   中英

Django view is not redirecting properly

I'm facing some difficulties to render pages with Django shortcuts. My workflow is quite simple:

  1. Users go a page where they can view some project data (detail_project)
  2. If they want to update the project data, they should click a button that will send a POST request and loads update_project page. This page is loaded with current project data.
  3. Users update and submit new data. After submission, they are returned to detail_project page.

My view is like this:

def update_project(request):
    if request.method == 'POST':
        if 'update_project_submit' in request.POST:
            # updates project with form data and returns to detail project page
            return redirect('detail_project', project_name=project_name)
        else:
            # loads project original data into the form 
    return render(request, 'project/update_project.html', context)


def detail_project(request, project_name):
    if request.method == 'POST':        
        if 'update_project' in request.POST:
            return update_project(request)
        else:
            # does another stuff
    else:
        # shows project details
        return render(request, 'project/detail_project.html', context)      

urls.py:

url(r'^project/update/$', views.update_project, name='update_project'),
url(r'^project/details/(?P<project_name>[a-zA-Z][a-zA-Z0-9-_]+)/$', views.detail_project, name='detail_project'),

And update_project.html:

<form class="form-horizontal" role="form" action="" method="post">
    {% csrf_token %}
    {% for field in form %}

        <div class="form-group">
            <div class="col-sm-3">
                <label for="{{ field.id_for_label }}" class="control-label">{{ field.label }}</label>
            </div>

            <div class="col-sm-9">
                {{field}}
            </div>
            <div class="col-sm-12">
                {{ field.help_text }}
            </div>
        </div>
        {{field.non_field_errors }}
        {{field.errors}}

    {% endfor %}


    <button type="submit" name="update_project_submit" class="btn btn-primary">Submit</button>

</form>

[ Update ]

Forms.py

class UpdateProjectForm(forms.Form):

    project_name_validator = RegexValidator(r'^[a-zA-Z][a-zA-Z0-9-_]{1,31}$', constants.PROJECT_NAME_INVALID)
    project_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'required': 'true'}), validators=[project_name_validator])
    project_description = forms.CharField(required=True, widget=forms.Textarea(attrs={'style':'resize:none;', 'required': 'true'}))
    project_expiration = forms.DateField(required=False, widget=forms.TextInput(attrs={'class':'datepicker'}))

    def __init__(self, *args, **kwargs):
        super(UpdateProjectForm, self).__init__(*args, **kwargs)
        self.fields['project_name'].label = "Project Name:"
        self.fields['project_description'].label = "Project Description:"
        self.fields['project_expiration'].label = "Expiration Date:"

The problem is that I cannot update my project. My page loads the form properly (update_project) with the current data (step 2), but when I submit it (click the Submit button, I'm redirected to detail project page without entering the if 'update_project_submit' in request.POST: statement. Maybe my workflow is wrong. I cannot figure it out.

I printed my request, and I've really cofirmed that when I submit the form, I'm receiving a POST request to detail_project.

Is there something I am missing? Or am I trying to do something wrong according to Django's logic?

Use

if request.POST.get('update_project_submit', False):

instead of

if 'update_project_submit' in request.POST:

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