html文件中的{{form}}未被呈现

时间:2018-05-16 07:06:49

标签: python django

urls.py

#python -m smtpd -n -c DebuggingServer localhost:1025
from django.contrib import admin
from .import views
from django.conf.urls import url,include
from django.contrib.auth.views import (
 login,logout,
 password_reset,
 password_reset_done,
 password_reset_confirm,
 password_reset_complete
 )
#from django.urls import path

urlpatterns = [
    url(r'^$',views.home),
    url(r'^login/$',login,{'template_name' : 'accounts/login.html'},name='login'),
    url(r'^logout/$',logout,{'template_name' : 'accounts/logout.html'}),
    url(r'^register/$',views.register,name='register'),
    url(r'^profile/$',views.view_profile,name='view_profile'),
    url(r'^profile/edit/$',views.edit_profile,name='edit_profile'),
    url(r'^change-password/$',views.change_password,name='change_password'),

    url(r'^reset-password/$',password_reset,{'template_name':'accounts/reset_password.html',
    'post_reset_redirect':'accounts:password_reset_done','email_template_name':'accounts/reset_password_email.html'},name='reset_password'),

    url(r'^reset-password/done/$',password_reset_done,{'template_name':'accounts/reset_password_done.html'},name='password_reset_done'),

    url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
    password_reset_confirm,{'template_name':'accounts/reset_password_confirm.html','post_reset_redirect':'accounts:password_reset_complete'},name='password_reset_confirm' ),

    url(r'^reset-password-complete/$',password_reset_complete,{'template_name':'accounts/reset_password_complete.html'},name='password_reset_complete'),



]

urls.py(教程)

from django.contrib import admin
from django.conf.urls import url,include
from tutorial import views
from django.conf import settings
from django.conf.urls.static import static
#from django.urls import path

urlpatterns = [
    url('admin/', admin.site.urls),
    url('accounts/',include('accounts.urls',namespace='accounts')),

    url(r'^$',views.login_redirect,name='login_redirect'),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

views.py

from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth.forms import UserCreationForm
from tutorial import views
from accounts.form import Registrationform,EditProfileForm
from django.contrib.auth.forms import UserChangeForm,PasswordChangeForm
from django.contrib.auth.models import User
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView
from .forms import HomeForm


class HomeView(TemplateView):
    def get(self,request):
        form = HomeForm(request.get)
        return render(request, 'accounts/home.html', {'form': form})


@login_required
def home(request):
    numbers=[1,2,3,4,5]
    name="jay"
    args={'myName':name}
    return render(request,'accounts/home.html',args)

def register(request):
    if request.method == "POST":
        form = Registrationform(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/accounts')
        else:
            return HttpResponse('please fill all the fields and make sure new password must be match')
    else:
        form = Registrationform()
        args={'form' : form}
        return render(request,'accounts/reg_form.html',args)
@login_required
def view_profile(request):
    args = {'user' : request.user}
    return render(request,'accounts/profile.html',args)
@login_required
def edit_profile(request):
    if request.user.is_authenticated():
        #<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
        if request.method=="POST":
            form = EditProfileForm(request.POST,instance=request.user)
            if form.is_valid():
                form.save()
                return redirect('/accounts/profile')
            else:
                return HttpResponse("please go back and write correct vaues")
        else:
            form  =  EditProfileForm(instance=request.user)
            args = {'form' : form}
            return render(request,'accounts/edit_profile.html',args)
    else:
        HttpResponse("hllo")
        return redirect('/accounts/login')
@login_required
def change_password(request):
    if request.user.is_authenticated():
        if request.method == "POST":
            form = PasswordChangeForm(data=request.POST,user=request.user)
            if form.is_valid():
                form.save()
                update_session_auth_hash(request, form.user)
                return redirect('/accounts/profile')
            else:
                return HttpResponse("password does not match,go back and try again")
        else:
            form = PasswordChangeForm(user=request.user)
            args = {'form' : form}
            return render(request,'accounts/change_password.html',args)
    else:
        return redirect ('/accounts/login')

forms.py

from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth.forms import UserCreationForm
from tutorial import views
from accounts.form import Registrationform,EditProfileForm
from django.contrib.auth.forms import UserChangeForm,PasswordChangeForm
from django.contrib.auth.models import User
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView
from .forms import HomeForm


class HomeView(TemplateView):
    def get(self,request):
        form = HomeForm(request.get)
        return render(request, 'accounts/home.html', {'form': form})


@login_required
def home(request):
    numbers=[1,2,3,4,5]
    name="jay"
    args={'myName':name}
    return render(request,'accounts/home.html',args)

def register(request):
    if request.method == "POST":
        form = Registrationform(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/accounts')
        else:
            return HttpResponse('please fill all the fields and make sure new password must be match')
    else:
        form = Registrationform()
        args={'form' : form}
        return render(request,'accounts/reg_form.html',args)
@login_required
def view_profile(request):
    args = {'user' : request.user}
    return render(request,'accounts/profile.html',args)
@login_required
def edit_profile(request):
    if request.user.is_authenticated():
        #<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
        if request.method=="POST":
            form = EditProfileForm(request.POST,instance=request.user)
            if form.is_valid():
                form.save()
                return redirect('/accounts/profile')
            else:
                return HttpResponse("please go back and write correct vaues")
        else:
            form  =  EditProfileForm(instance=request.user)
            args = {'form' : form}
            return render(request,'accounts/edit_profile.html',args)
    else:
        HttpResponse("hllo")
        return redirect('/accounts/login')
@login_required
def change_password(request):
    if request.user.is_authenticated():
        if request.method == "POST":
            form = PasswordChangeForm(data=request.POST,user=request.user)
            if form.is_valid():
                form.save()
                update_session_auth_hash(request, form.user)
                return redirect('/accounts/profile')
            else:
                return HttpResponse("password does not match,go back and try again")
        else:
            form = PasswordChangeForm(user=request.user)
            args = {'form' : form}
            return render(request,'accounts/change_password.html',args)
    else:
        return redirect ('/accounts/login')

home.html的

{% extends 'base.html' %}


<!DOCTYPE html>

  <head>
    {% block head %}
    <title>home</title>
    {% endblock %}
  </head>

  {% block body %}
    <div class="container">

    <form  method="post">
      {% csrf_token %}
      {{ form.as_p  }}

      <button type="submit" >Submit</button>
    </form>
  </div>

{% endblock %}
</html>

和浏览器网址

enter image description here

我的问题是home.html文件中的{{form}}没有呈现。 {{form.as_p}}也无效。

表单标签(输入)无法出现在网页(浏览器)中。

并且终端中没有显示任何错误。

请帮我找错。

1 个答案:

答案 0 :(得分:1)

它通过改变类来运作。

def HomeView(request):
    form = HomeForm()
    return render(request, 'accounts/home.html', {'form': form})

也改变了

url(r'^$',views.HomeView)
在urls.py中