{%include tag%}仅包含另一个模板中的一个对象

时间:2018-07-12 13:38:13

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

我需要一点帮助 这是我的views.py

def signup(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        verification=VerificationForm(request.POST)
        if form.is_valid():
            userObj = form.cleaned_data
            username = userObj['username']
            email =  userObj['email']
            password =  userObj['password']
            return HttpResponseRedirect('/index/verification/')
            # if
            if not (User.objects.filter(username=username).exists() or User.objects.filter(email=email).exists()):
                User.objects.create_user(username, email, password)
                user = authenticate(username = username, password = password)
                login(request, user)
                return HttpResponseRedirect('/')
            else:
                raise forms.ValidationError('Looks like a username with that email or password already exists')
        else:

            raise forms.ValidationError('a valid')

    else:
        form = UserRegistrationForm()
        verification = VerificationForm()

    return render(request, 'question/signup.html',context= {'verification':verification,'form' : form})

在这里您可以看到我已经声明了两个上下文变量,一个是表单,另一个是现在要使用的验证 现在这是我的signup.html

{% extends 'question/index.html '%}

{% block body_block %}
<div class="conrainer">


  <form method="POST">
      {% csrf_token %} {{ form.as_p }}
      <button type="submit">Submit</button>
  </form>
  <a href="{% url 'social:begin' 'facebook'%}">facebook auth</a>

</div>
{% endblock %}

现在我已在此页面中使用过表单变量
我想在另一页中使用验证变量,因为django视图未在单个视图中使用两个模板 在网上论坛中搜索了很多之后,我才知道{%include tag%} 在阅读完文档后尝试使用该标签后 这就是我的Verification.html的方式

{% include 'question/signup.html' with obj=verification only%}
<div class="container">

<form  method="post">{% csrf_token %}
<p>{{verification.as_p}}</p>
<p>{{obj.as_p}}</p>




<input type="submit" name="verify" value="" action='.' align='center'name='submit'>
</form>
</div>

这是我的urls.py文件

urlpatterns = [
    # url(r'^/',views.home,name='home'),
    url(r'^home/',Home,name='home'),
    url(r'^ques/',Create.as_view(success_url="/index/home/"),name='ques'),
    url(r'^signup/',signup,name='signup'),
    # url(r'^signup/',MyFormView.as_view(),name='signup'),
    url(r'^verification/',TemplateView.as_view(template_name="question/verification.html")),

    url(r'^logout/$', auth_views.logout,name='logout'),
    url(r'^search/',Search,name='search'),
    url(r'^accounts/', include('allauth.urls')),

    # url(r'^verify/',verificationView,name='signup'),


    # CreateView.as_view(model=myModel, success_url=reverse('success-url'))

]

,但是尝试了很多方法后,无论是obj还是验证都显示在网站上,有什么方法我只能获取上下文对象,而不是获得subit按钮,并且使用INCLUDE可以通过我的signup.html获得所有帮助受到赞赏

0 个答案:

没有答案