我使用html格式创建登录页面: 在这里,我想将用户注册为管理员。 但是当我提交注册表时,它什么都没有显示(空白页)
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'music/form-template.html' %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<button type="submit" class="btn-success">Register</button>
</div>
</div>
</form>
和forms.py
:
from django.contrib.auth.models import User
from django import forms
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email', 'password']
这是url.py
from django.urls import re_path, path
from . import views
app_name = 'music'
urlpatterns=[
re_path(r'^$',views.IndexView.as_view(), name='index'),
path(r'register/',views.UserFormView.as_view(), name='register'),
re_path(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(),name='details'),
path(r'album/add/', views.AlbumCreate.as_view(), name='album-add'),
re_path(r'album/(?P<pk>[0-9]+)/$', views.AlbumUpdate.as_view(),name='album-update'),
re_path(r'album/(?P<pk>[0-9]+)/delete/$', views.AlbumDelete.as_view(),name='album-delete'),
]
这是主要的url.py:
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include,path
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('music/',include('music.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
当然还有views.py
:
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views import generic
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.urls import reverse_lazy
from django.views.generic import View
from .forms import UserForm
from .models import Album
class IndexView(generic.ListView):
template_name = 'music/index.html'
context_object_name = 'all_album'
def get_queryset(self):
return Album.objects.all()
class DetailView(generic.DetailView):
model = Album
template_name = 'music/details.html'
class AlbumCreate(CreateView):
model = Album
fields = ['artist', 'album_title', 'genre', 'album_logo']
class AlbumUpdate(UpdateView):
model = Album
fields = ['artist', 'album_title', 'genre', 'album_logo']
class AlbumDelete(DeleteView):
model = Album
success_url = reverse_lazy('music:index')
class UserFormView(View):
form_class = UserForm
template_name = 'music/registration_form.html'
# display blank form
def get(self, request):
form = self.form_class(None)
return render(request,self.template_name, {'form':form})
# process form data
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
# cleaned (normalized) data
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
# returns user objects if credentials are correct
user = authenticate(username=username, password= password)
if user is not None:
if user.is_active():
login(request, user)
return redirect('music:index')
return render(request,self.template_name, {'form':form})
但提交表单后应该出现错误:
Method Not Allowed (POST): /music/register/
[26/Mar/2018 19:07:58] "POST /music/register/ HTTP/1.1" 405 0
因为我刚接触Django所以我不太了解错误处理。
请帮我解决问题。 谢谢
答案 0 :(得分:1)
您已将视图调用为UserFormView,但它实际上并不是表单视图;它只是一个标准的视图,它不知道如何处理POST。
您应该像其他视图那样继承CreateView或UpdateView。