我正在尝试使用Django实现博客应用。我用个人资料照片上传创建了注册表。但是,在我输入数据并选择图像后,单击“提交”而不提交数据。它返回相同的注册页面。而不是提交数据。
#models.py
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg',upload_to='pics')
#views.py
def register(request):
if request.method == "POST":
form = Register(request.POST,request.FILES)
if form.is_valid():
profile = Profile()
email = form.cleaned_data['Email']
User_name=form.cleaned_data['Username']
Password=form.cleaned_data['Password']
Confirm_Password=form.cleaned_data['Confirm_Password']
firstname=form.cleaned_data['Firstname']
user=User.objects.create_user(username=User_name,
password=Password,email=email,first_name=firstname)
user.save();
profile.image = form.cleaned_data["Image"]
profile.save();
return redirect('/')
else:
form = Register()
return render(request,'register.html',{'form': form})
#forms.py
class Register(forms.Form):
Email = forms.EmailField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}))
Username = forms.CharField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}))
Password = forms.CharField(widget=forms.PasswordInput(attrs=
({"class":"inputvalues"})))
Firstname = forms.CharField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}),max_length=30)
Lastname = forms.CharField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}),max_length=40)
Confirm_Password = forms.CharField
(widget=forms.PasswordInput(attrs=({"class":"inputvalues"})))
Image = forms.ImageField()
def clean_Email(self):
if validate_email(self.cleaned_data['Email']):
raise forms.ValidationError("Email is not in correct format!")
elif User.objects.filter(email = self.cleaned_data['Email'])
.exists():
raise forms.ValidationError("Email aready exist!")
return self.cleaned_data['Email']
def clean_Username(self):
if User.objects.filter(username =
self.cleaned_data['Username']).exists():
raise forms.ValidationError("Username already exist!")
return self.cleaned_data['Username']
def clean_Confirm_Password(self):
pas=self.cleaned_data['Password']
cpas = self.cleaned_data['Confirm_Password']
if pas != cpas:
raise forms.ValidationError("Password and Confirm Password are not
matching!")
else:
if len(pas) < 8:
raise forms.ValidationError("Password should have atleast 8
character")
if pas.isdigit():
raise forms.ValidationError("Password should not all numeric")
<!-------register.html>
{% extends 'layout.html' %}
{% block content %}
<div class="box">
<h2>
<center>Register</center>
</h2><br>
<form action='register' method='POST'>
{% csrf_token %}
<div>
<label class='labe'>Image:</label>
{{ form.Image }}
</div>
<div>
<label class='labe'>First name:</label>
{{ form.Firstname }}
</div>
<div>
<label class='labe'>Last name:</label>
{{ form.Lastname }}
</div>
<div>
<label class='labe'>Email:</label>
{{ form.Email }}
{{ form.Email.errors }}
</div>
<div>
<label class='labe'>Username:</label>
{{ form.Username }}
{{ form.Username.errors }}
</div>
<div>
<label class='labe'>Password:</label>
{{ form.Password }}
</div>
<div>
<label class='labe'>Confirm Password:</label>
{{ form.Confirm_Password }}
{{ form.Confirm_Password.errors }}
</div>
<input type="Submit" id="lg"/><br>
<center><a href="login" >Already have an account.Login here.
</a></center>
</form>
</div>
<div>
{% for message in messages%}
<h1>{{message}}</h1>
{% endfor %}
</div>
{% endblock %}
返回相同的注册页面。而不是提交数据。
答案 0 :(得分:0)
您的表单必须为<form enctype="multipart/form-data" method="POST">
MDN:https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST