我正在做一个项目,我不确定django是否可以做我想做的事情。如果是,请指导我。
在这里:
在我的应用程序的注册和登录功能中,我将注册设计为可以完成注册和登录的工作(即,当用户填写注册表格并且数据与其他用户的数据(尤其是用户名)不冲突时,新用户将自动注册并登录)。
这是我到目前为止所拥有的。
views.py:
func inTimeSpan(start, end, check time.Time) bool {
return check.After(start) && check.Before(end)
}
func main() {
now := time.Now()
newLayout := "15:04"
ns, _ := time.Parse(newLayout, strconv.Itoa(now.Hour())+":"+strconv.Itoa(now.Minute()))
srt, _ := time.Parse(newLayout, "23:00")
end, _ := time.Parse(newLayout, "05:00")
fmt.Println("1 : ", inTimeSpan(srt, end, ns))
}
注册表格(register.html)
def register(request):
"""This view takes care of registering the user and logging in the user at the same time"""
if request.method == 'POST':
form = UserNameFormRegistration(request.POST, request.FILES)
if form.is_valid():
form.save()
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
the_email = form.cleaned_data['email']
the_first_name = form.cleaned_data['first_name']
the_last_name = form.cleaned_data['last_name']
profile_picture = form.cleaned_data['profile_picture']
user = authenticate(username=username, password=password)
user.email = the_email
user.first_name = the_first_name
user.last_name = the_last_name
login(request, user)
return redirect(request.META.get('HTTP_REFERER'))
else:
form = UserCreationForm()
context = {
'form': form,
}
return render(request, 'registration/register.html', context)
当然还有urls.py:
<!-- form -->
<div class="limiter">
<div class="container-login100">
<div class="wrap-login100">
<form class="login100-form validate-form" action="{% url 'register' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<span class="login100-form-title p-b-34">
<b><font style = "font-family:Open Sans">Sign Up</font></b>
</span>
<!-- first name -->
<div class="wrap-input100 rs1-wrap-input100 validate-input m-b-20" data-validate="Type first name">
<input class="input100" type="text" name="first_name" placeholder="First name" id='{{ form.first_name }}'>
<span class="focus-input100"></span>
</div>
<!-- /first name -->
<!-- last name -->
<div class="wrap-input100 rs2-wrap-input100 validate-input m-b-20" data-validate="Type last name">
<input class="input100" type="text" name="last_name" placeholder="Last name" id='{{ form.last_name }}'>
<span class="focus-input100"></span>
</div>
<!-- /last name -->
<!-- email address -->
<div class="wrap-input100 rs3-wrap-input100 validate-input m-b-20" data-validate="Type email">
<input id='{{ form.email }}' class="input100" type="text" name="email" placeholder="email address">
<span class="focus-input100"></span>
</div>
<!-- /email address -->
<!-- username -->
<div class="wrap-input100 rs3-wrap-input100 validate-input m-b-20" data-validate="Type user name">
<input id='{{ form.username }}' class="input100" type="text" name="username" placeholder="User name">
<span class="focus-input100"></span>
</div>
<!-- username -->
<!-- password ( or password 1 )-->
<div class="wrap-input100 rs4-wrap-input100 validate-input m-b-20" data-validate="Type password">
<input class="input100" type="password" name="password1" placeholder="Password" id='{{ form.password1 }}'>
<span class="focus-input100"></span>
</div>
<!-- /password (or password 1 )-->
<!-- password validate ( or password 2 ) -->
<div class="wrap-input100 rs4-wrap-input100 validate-input m-b-20" data-validate="Type password">
<input class="input100" type="password" name="password2" placeholder="Password confirmation" id='{{ form.password2 }}'>
<span class="focus-input100"></span>
</div>
<!-- /password validate ( or password 2 ) -->
{{ form.errors }}
<!-- profile picture upload -->
<div class="container kv-main">
<br/><input id="{{ form.profile_picture }}" class="file" type="file" name="profile_picture"> <br/>
</div>
<!-- /profile picture upload -->
<div class="container-login100-form-btn">
<button class="login100-form-btn" value="Register" type="submit">
Sign Up
</button>
<input type="hidden" name="next" value="{{ request.META.HTTP_REFERER }}">
</div>
<div class="w-full text-center p-t-27 p-b-239">
<span class="txt1">
Forgot
</span>
<a href="#" class="txt2">
User name / password?
</a>
</div>
<div class="w-full text-center">
<a href="/accounts/login" class="txt3">
<h3><font style = "font-family:Poppins">Already have an account?</font></h3>
login
</a>
</div>
</form>
<div class="login100-more" style="background-image: url('{% static 'images/bg-01.jpg' %}');"></div>
</div>
</div>
</div>
<!-- /form -->
问题: 这里的问题在于,填写注册表并提交数据后,该应用程序不会重定向到register.html视图之前查看的上一页。
我已经雇用
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^index/', 'blog.views.index'),
url(r'^author/$', 'blog.views.author_page'),
url(r'^about/$', 'blog.views.about_page'),
url(r'^blank/$', 'blog.views.blank_page'),
url(r'^post/(?P<id>\d+)/$', 'blog.views.blogpost_page'),
url(r'^category/(?P<category>\w+)/$', 'blog.views.category_page'),
url(r'^contact/$', 'blog.views.contact_page'),
url('accounts/', include('django.contrib.auth.urls')),
url('register', 'ixorabloom_users.views.register', name='register'),
url('news_letter', 'blog.views.news_letter', name='news_letter'),
url('(?P<username>\w+)/profile', 'blog.views.user_profile', name='profile'),
]
但是它仍然无法完成工作。它只是重定向到填写注册表的页面(即注册视图)。顺便说一下,这是django 1.8.5。谢谢,希望得到您的答复。我真的希望这是可能的。