how to get POST data in django 1.3

后端 未结 4 900
感情败类
感情败类 2021-02-06 02:49

Hey, I am following this tutorial to learn to make a wiki page with Django. However, it is made in django 0.96 and I use Django 1.3 so there are some things that are different.

相关标签:
4条回答
  • 2021-02-06 03:34

    I guess you've missed the symbol '=' in the form declaration.

    action"/wikicamp/{{page_name}}/save/"
    
    action="/wikicamp/{{page_name}}/save/"
    

    Fortunately, it might be not a mistake. So if it is not a solution, try some more easy example:

    # settings.py
    
    TEMPLATE_DIRS = (
        # Here comes something like "C:/www/django/templates"
    )
    
    MIDDLEWARE_CLASSES = (
        ...
        'django.middleware.csrf.CsrfViewMiddleware',
        ...
    )
    
    # urls.py
    
    urlpatterns = patterns('',
        ('^foo', foo),
    )
    
    
    # views.py
    from django.http import HttpResponse
    from django.shortcuts import render_to_response
    from django.core.context_processors import csrf
    
    def foo(request):
        d = {}
        d.update(csrf(request))
        if 'output' in request.POST:
            d.update({'output':request.POST['output']})
        return render_to_response('foo.html',d)
    
    # foo.html template
    <html>
    <h1> Foo </h1>
    <form action="/foo" method = "post">
        {% csrf_token %}
        <input type="text" name="output"></input>
        <input type="submit" value="go"></input>
    </form>
    <p> Output: {{ output }} </p>
    </html>
    

    Hope this will work

    0 讨论(0)
  • 2021-02-06 03:38

    You've got to include {% csrf_token %} in your form's template between your <form> tags.

    <form method="post" action"/wikicamp/{{page_name}}/save/">
        {% csrf_token %}
        <textarea name="content" rows="20" cols="60">{{content}}</textarea><br>
        <input type="submit" value="Save Page"/>
    </form>
    

    If the csrf_token is not rendered into your form make sure you're providing the RequestContext in the view's response:

    from django.shortcuts import render_to_response
    from django.template import RequestContext
    
    def app_view(request):
        return render_to_response('app_template.html', 
                                  app_data_dictionary, 
                                  context_instance=RequestContext(request))
    

    Or, use this shortcut method:

    from django.views.generic.simple import direct_to_template
    
    def app_view(request):             
        return direct_to_template(request, 'app_template.html', app_data_dictionary)
    

    The RequestContext is always available when you're using generic views.

    0 讨论(0)
  • 2021-02-06 03:43

    You will need the {% csrf_token %} template tag in between your tags as well as including

       django.middleware.csrf.CsrfViewMiddleware
       django.middleware.csrf.CsrfResponseMiddleware
    

    in your MIDDLEWARE_CLASSES in the applications settings.py

    Adding some example post data handling:

    This is an example of one of the times I am using POST data in a view. I will generally rely on the form class to do verification via the cleaned_data array.

    if request.method == 'POST':
            form = ForgotPassword(data=request.POST)
            if form.is_valid():
                try:
                    new_user = backend.forgot_password(request, **form.cleaned_data)
                except IntegrityError:
                    context = {'form':form}
                    form._errors[''] = ErrorList(['It appears you have already requested a password reset, please \
                    check ' + request.POST['email2'] + ' for the reset link.'])
                    return render_template(request,'passwordReset/forgot_password.html',context)
                if success_url is None:
                    to, args, kwargs = backend.post_forgot_password(request, new_user)
                    return redirect(to, *args, **kwargs)
                else:
                    return redirect(success_url)
    
    0 讨论(0)
  • 2021-02-06 03:43

    re above use "request.POST" not "c.POST" in the 3rd line

    def save_page (request,page_name):
        content = request.POST["content"]
    

    and change in "edit_page"

    -   return render_to_response("edit.html",{"page_name":page_name, "content":content})
    +   t = get_template('edit.html')
    +   html = t.render(Context({"page_name":page_name, "content":content}))
    +   return HttpResponse(html)
    

    - :remove 
    + :add
    
    0 讨论(0)
提交回复
热议问题