简体   繁体   English

从视图来看,如何将上下文和注册表格传递给我的注册模板?

[英]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.

相关问题 DJANGO:如何在我的注册表中订购输入内容? - DJANGO: How do I order my input in my Registration Form? Django:如何传递上下文变量以进行注册还原? - Django: How to pass in context variables for registration redux? 在 python 中要求客户注册时,如何防止文本文件中的重复 - How do I prevent duplication in my text file when asking for registration from customer in python 如何在Django视图中使用注册(注册/登录)模式(引导模板)? - How to use registration(signup/login) modal(bootstrap template) with django views? 将上下文传递给Django注册的观点 - Passing context to django-registration's views 为什么我的Django注册表格未通过is_valid() - Why my django registration form does not pass is_valid() django-registration将extra_context传递给注册表单 - django-registration passing extra_context to Registration Form 如何将设置TEMPLATE_CONTEXT_PROCESSORS中定义的上下文传递给我的自定义视图 - How to pass context defined in setting TEMPLATE_CONTEXT_PROCESSORS to my custom views 我如何清除此字段是必需的表单错误,用于在注册表单中上传文件 - How do i clear a this field is required form error for uploading a file in a registration form 如何更改django-registration电子邮件模板“站点”名称? - How do I change the django-registration e-mail template “site” name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM