標籤:header image ide no-repeat pos port add lock utf-8
models.py:
# 使用者資訊表class UserProfile(models.Model): belong_to = models.OneToOneField(to=User, related_name="profile") # 所屬使用者 avatar = models.FileField(upload_to=‘avatar‘) # 帳戶圖片 def __str__(self): return self.belong_to.username
views.py:
def register(request): if request.method == ‘GET‘: form = RegisterForm() if request.method == ‘POST‘: form = RegisterForm(request.POST) print(‘ppppost‘) if form.is_valid(): username = form.cleaned_data.get("username") email = form.cleaned_data.get("email") password = form.cleaned_data.get("password") user = User(username=username,email=email) user.set_password(password) user.save() #建立使用者儲存 userprofile = UserProfile(belong_to=user,avatar=‘avatar/avatar.png‘) userprofile.save() #建立該使用者的資料 return redirect(to=‘login‘) else: print(form.errors) context={} context[‘form‘]=form return render(request,‘register.html‘,context=context)
forms.py:
class RegisterForm(forms.Form): username = forms.CharField(widget=forms.TextInput(attrs={‘placeholder‘:‘使用者名稱‘})) email = forms.CharField(widget=forms.TextInput(attrs={‘placeholder‘:‘郵箱‘})) password = forms.CharField(widget=forms.PasswordInput(attrs={‘placeholder‘:‘密碼‘})) password_confirm = forms.CharField(widget=forms.PasswordInput(attrs={‘placeholder‘:‘確認密碼‘})) def clean(self): cleaned_data = super(RegisterForm,self).clean() username = cleaned_data.get("username") email = cleaned_data.get("email") password = cleaned_data.get("password") password_confirm = cleaned_data.get("password_confirm") if User.objects.filter(username=username): raise forms.ValidationError("使用者已存在") if User.objects.filter(email=email): raise forms.ValidationError("該郵箱已被註冊") try: validate_email(email) except ValidationError: raise forms.ValidationError("不正確的郵箱格式") if len(password) < 6: raise forms.ValidationError("密碼長度至少6位") if password_confirm != password: raise forms.ValidationError("兩次輸入的密碼不一致")
模板css register.css:
body{ background-image:url(../images/login/backgroundimg.png); background-size: cover; background-repeat: no-repeat;}.ui.basic.segment.container.content{ width: 425px; margin-left: 50%; margin-top: 389px;}.ui.header{ color:red; font-size: 50px !important; height: 75px;}input{ border-radius: 31.5px!important; height: 31px!important; background-color: #ffffff!important; border: solid 1px #ededed!important;}.field{ position: relative;}.ui.image.top{ position: absolute; left: -20px; top:10px;}.ui.red.button.register{ border-radius: 32px; height: 30px; width: 400px; padding-top: 8px;}.ui.basic.segment.container.footer{ position: relative; margin-top: 150px;}.ui.circular.red.button.backtoindex{ width: 31px!important; height: 62px; border-radius: 15.5px; background-color: #f30c28; position: absolute; transform: translate(-50%,-50%); left: 50%; top:30px; padding: 0;}img{ margin-left: 2px;}h4{ font-size: 12px!important; color: #ffffff; margin-left: 2px;}.ui.image.title{ position: relative; transform: translate(-50%); left: 50%; top:45px;}
模板檔案 register.html:
<!DOCTYPE html>{% load staticfiles %}<html> <head> <meta charset="utf-8"> <title>註冊頁</title> <link rel="stylesheet" href="{% static ‘css/semantic.css‘ %}" media="screen" title="no title" charset="utf-8"> <link rel="stylesheet" href="{% static ‘css/register.css‘ %}" media="screen" title="no title" charset="utf-8"> </head> <body> <div class="ui basic segment container content"> <h1 class="ui center aligned header">註冊</h1> <form class="ui form error" method="post"> {% if form.errors %} <div class="ui error message"> {{ form.errors }} </div> {% endif %} <div class="field"> <div class="ui image top"> <img src="{% static ‘images/login/usericon.png‘ %}" /> </div> {{form.username}} </div> <div class="field"> <div class="ui image top"> <img src="{% static ‘images/login/checkboxicon.png‘ %}" /> </div> {{form.email}} </div> <div class="field"> <div class="ui image top"> <img src="{% static ‘images/login/lockicon.png‘ %}" /> </div> {{form.password}} </div> <div class="field"> <div class="ui image top"> <img src="{% static ‘images/login/lockicon.png‘ %}" /> </div> {{form.password_confirm}} </div> {% csrf_token %} <div class="field"> <button type="submit" class="ui red button register">註冊</button> </div> </form> </div> <div class="ui basic segment container footer"> <div class="bottom"> <div class="ui divider"></div> <button type="button" name="button" class="ui circular red button backtoindex"> <img src="{% static ‘images/login/homeicon.png‘ %}" style="position:absolute;left:18%;top:10%;"> <h4>首頁</h4> </button> <div class="ui image title"> <img src="{% static ‘images/login/zhiribao.png‘ %}" /> </div> </div> </div> </body></html>
urls.py:
url(r‘^register/‘, register, name=‘register‘)
新聞網站項目django--註冊頁