1、引用静态文件:在html页面引入{% load staticfiles %} ,然后在静态文件路径加上href="{% static ‘css/reset.css‘ %}"即可,static会自动到我们的setting.py文件中找到我们设置的STATIC_URL = ‘/static/‘,也就是static会自动在静态路径的前面加上static
2、注册表单 form验证:
class RegisterForm(forms.Form):
email = forms.EmailField(required=True)
password = forms.CharField(required=True, min_length=5)
captcha = CaptchaField(error_messages={‘invalid‘:u‘验证码错误‘})
在user app的view中编写注册view:RegisterView
from django.contrib.auth.hashers import make_password
class RegisterView(View):
def get(self,request):
registerform = RegisterForm()
return render(request, "register.html", {‘registerform‘:registerform})
def post(self,request):
registerform = RegisterForm(request.POST)
if registerform.is_valid(): #注册流程
user_name = request.POST.get(‘email‘, ‘‘)
pass_word = request.POST.get(‘password‘, ‘‘)
user_profile = UserProfile()
user_profile.username = user_name
user_profile.email = user_name
user_profile.password = make_password(pass_word)
user_profile.is_active = False
user_profile.save()
send_email(user_name, ‘register‘)
return render(request, "login.html")
else:
return render(request, "register.html", {‘registerform‘:registerform})
html页面:
<div class="tab-form">
<form id="email_register_form" method="post" action="{% url ‘register‘ %}" autocomplete="off">
<input type=‘hidden‘ name=‘csrfmiddlewaretoken‘ value=‘gTZljXgnpvxn0fKZ1XkWrM1PrCGSjiCZ‘ />
<div class="form-group marb20 {% if registerform.errors.eamil %} errorput {% endif %}">
<label>邮 箱</label>
<input type="text" id="id_email" name="email" value="{{ registerform.email.value }}" placeholder="请输入您的邮箱地址" />
</div>
<div class="form-group marb8 {% if registerform.errors.password %} errorput {% endif %} ">
<label>密 码</label>
<input type="password" id="id_password" name="password" value="{{ registerform.password.value }}" placeholder="请输入6-20位非中文字符密码" />
</div>
<div class="form-group marb8 captcha1 {% if registerform.errors.captcha %} errorput {% endif %} ">
<label>验 证 码</label>
{{ registerform.captcha }}
</div>
<div class="error btns" id="jsEmailTips">{% for key,error in registerform.errors.items %}{{ error }}{% endfor %}{{ msg }}</div>
<div class="auto-box marb8">
</div>
<input class="btn btn-green" id="jsEmailRegBtn" type="submit" value="注册并登录" />
<input type=‘hidden‘ name=‘csrfmiddlewaretoken‘ value=‘5I2SlleZJOMUX9QbwYLUIAOshdrdpRcy‘ />
{% csrf_token %}
</form>
</div>