django用户注册

Posted onlyhold

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django用户注册相关的知识,希望对你有一定的参考价值。

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>邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱</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>密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码</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>验&nbsp;证&nbsp;码</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>

以上是关于django用户注册的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Django Summernote 中显示编程片段的代码块?

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

[TimLinux] Django 信号

如何在 Django 中显式重置模板片段缓存?

python 通过django片段很多很多