[英]From views, how do I pass both context and registration form to my registration template?
I'm fairly new to Django and I'm trying to pass both the context and my registration form. 我是Django的新手,我想同时传递上下文和注册表格。 I know how to pass either the context, or the form, but not both.
我知道如何传递上下文或形式,但不能同时传递两者。 Check the last line of the code, that's what I'm trying to figure out.
检查代码的最后一行,这就是我要弄清楚的内容。
I've tried: 我试过了:
return render(request, 'users/register.html', context, {'form': form}) 返回render(请求,'users / register.html',上下文,{'form':form})
and it doesn't work. 而且不起作用。 There's something wrong with the syntax.
语法有问题。
from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm
def register(request):
context = {
'title': "Register",
'page_title': "Golf App - Register",
'login_title': "Login"
}
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Account Created for {username}!')
return redirect('golf-home')
else:
form = UserRegisterForm()
return render(request, 'users/register.html', {'form': form})
I'm trying to get it so that I can use both the context and form in the template. 我正在尝试获取它,以便可以在模板中使用上下文和表单。
只需将表单放在上下文中
context['form'] = UserRegisterForm()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.