渲染django无法传递参数

时间:2015-03-07 13:25:03

标签: django django-forms django-templates django-views

我是django的新手,我正在尝试创建一个应用程序,用户可以根据他/她的ID更改他/她的密码。到目前为止还不错,但是当我试图从我的views.py中传递一个参数时,一个参数无法传递给html模板。

这是我的观点:

def tambah_user(request, template_name='form_user.html'):
    if 'username' in request.session:
        username = User.objects.all()
        data = {}
        data['object_list'] = username
        if request.method == 'POST':
            user_form = UserForm(data=request.POST)
            profile_form = UserProfileForm(data=request.POST)

            if user_form.is_valid() and profile_form.is_valid():
                user = user_form.save()
                user.set_password(user.password)
                user.save()
                profile = profile_form.save(commit=False)
                profile.user = user
                profile.save()
                return redirect('manajemen_user')
            else:
                print user_form.errors, profile_form.errors
        else:
            user_form = UserForm()
            profile_form = UserProfileForm()

        return render(request, template_name,
                {'user_form': user_form, 'profile_form': profile_form, 'data': data},)
    else:
        return HttpResponseRedirect('/simofa/login')

使用该代码,data无法传递给html模板。

当我像这样改变渲染时

return render(request, template_name,data)

它工作正常,但user_form和profile_form没有传递给模板。

我的问题:

  1. 我可以将data, user_form, profile_form一起传递给模板吗?
  2. 如果可以,怎么样?如果没有,还有其他办法吗?
  3. 我非常感谢您的意见。所以请帮帮我。谢谢^ __ ^

1 个答案:

答案 0 :(得分:1)

当你定义一个元组时,render语句不会期待一个尾随逗号,这很可能是导致错误的原因,但是你的代码可能会使用一些重构....

from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

# assuming you're using your own form here for some reason...
from .forms import UserForm

# use the login_required decorator instead of manually checking for the session
@login_required
def tambah_user(request, template_name='form_user.html'):
    # `users` just makes more sense here for the variable name
    users = User.objects.all()

    # no need for the extra if clause to create unbound forms
    user_form = UserForm(request.POST or None)
    profile_form = UserProfileForm(request.POST or None)

    if request.method == 'POST':
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()

            # you need to pass in the raw password here,
            # otherwise you're hashing the already hashed password
            # if you were to be editing an instance
            user.set_password(user_form.cleaned_data('password'))
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()

            # you need to use reverse by name to do this
            return redirect(reverse('manajemen_user'))
        else:
            print user_form.errors, profile_form.errors

    # notice I have omitted the trailing comma after the context
    # dictionary
    return render(request, template_name, {'user_form': user_form,
        'profile_form': profile_form, 'data': users})

我希望您已经阅读过Django中的documentation for authentication,因为有内置的表单和视图可以为用户完成您需要做的所有事情,而不是自己编写。