我目前在Web应用程序的“ myaccount.html”页面上有一个表单。
此表单呈现了我想要的样子,但是由于某种原因我无法提交表单值以使其出现在django管理员中。
当用户单击“提交”时,页面将刷新,并且值保留在输入字段中。
我尝试了许多方法来解决此问题,但没有运气。我以为我把所有东西都放在一起了,但是也许我在某处缺少了东西?
有什么我需要解决的问题吗?
我是Django的新手,非常感谢您的帮助。
谢谢!
from users.forms import CustomUserCreationForm
from django.views.generic.edit import CreateView
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_GET, require_POST
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
from django.shortcuts import render, redirect
from django.template import loader
from django.urls import reverse_lazy
from django.views import generic
from django.views.generic import FormView
from django.views.generic.edit import FormView
from django.conf import settings
from django import forms
class SignUp(generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('home')
template_name = 'signup.html'
class change(generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('myaccount')
template_name = 'myaccount.html'
def form_valid(self, form):
form.instance.name = self.request.name
form.instance.address = self.request.address
form.instance.zip_code = self.request.zip_code
form.instance.mobile_number = self.request.mobile_number
form.save()
return super(change, self).form_valid(form)
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
class CustomUserManager(UserManager):
def get_by_natural_key(self, username):
case_insensitive_username_field = '{}__iexact'.format(self.model.USERNAME_FIELD)
return self.get(**{case_insensitive_username_field: username})
#class CustomUser(AbstractUser):
# objects = CustomUserManager()
class CustomUser(AbstractUser):
# add additional fields in here
name = models.CharField(max_length=100, null=True, blank=True)
address = models.CharField(max_length=100, null=True, blank=True)
zip_code = models.CharField(max_length=100, null=True, blank=True)
mobile_number = models.CharField(max_length=100, null=True, blank=True)
objects = CustomUserManager()
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
name = forms.CharField(required=True, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Full Name', 'class': 'form-control'}))
address = forms.CharField(required=True, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Address', 'class': 'form-control'}))
zip_code = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Zipcode', 'class': 'form-control'}))
mobile_number = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Mobile Number', 'class': 'form-control'}))
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = ('username', 'email', 'name', 'address', 'zip_code', 'mobile_number')
help_texts = {
'username': '',
'email': '',
# 'password1': 'None',
# 'password2': 'Test'
}
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('username', 'email')
from django.conf.urls import url
from django.conf.urls import *
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from . import views
from users import views
from .views import change
urlpatterns = [
# path('change/', views.HomeView.as_view(), name='change'),
# path('myaccount/change/', HomeView.as_view(), name='change'),
# url(r'^myaccount/change/$', views.horse, name='change'),
path('signup/', views.SignUp.as_view(), name='signup'),
path('myaccount/', views.change.as_view(), name='myaccount'),
]
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['email', 'username', 'name', 'address', 'zip_code', 'mobile_number']
admin.site.register(CustomUser, CustomUserAdmin)
HTML
{% block content %}
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.name }}
{{ form.name.errors }}
{{ form.address }}
{{ form.address.errors }}
{{ form.zip_code }}
{{ form.zip_code.errors }}
{{ form.mobile_number }}
{{ form.mobile_number.errors }}
<button class="btn btn-primary btn-success btn-round btn-extend" type="submit" value="Submit"><i class="zmdi zmdi-favorite-outline6"></i>Submit</button>
</form>
</div>
{% endblock content %}
答案 0 :(得分:2)
您没有显示从父类继承的字段或字段的错误。没有它们,该表格将无效。尝试添加{{ form.errors }}
以查看验证错误的完整列表。
答案 1 :(得分:1)
对于您的form_valid方法,请尝试以下操作:
if (i != lenStringOfLetters-1) {
对于模板,您还可以使用for循环来减少代码:
def form_valid(self, form):
user = form.save(commit=False) # object with form data
user.save() # save object
return redirect('success') # and then redirect to success view