[英]Creating a simple multiple users app in django
So I've got three users, a teacher
, student
, and admin
.所以我有三个用户,一个
teacher
、 student
和admin
。 Both teacher
and student
users work fine but when I try to login using the admin
form, it redirects to the student
login form. teacher
和student
用户都可以正常工作,但是当我尝试使用admin
表单登录时,它会重定向到student
登录表单。 I think it's because there's something wrong with the urls.py
and the way the next
parameter is configured but I'm not quite sure where to proceed.我认为这是因为
urls.py
和next
参数的配置方式有问题,但我不太确定从哪里开始。
Any help is much appreciated.任何帮助深表感谢。 Let me know if you need more information
如果您需要更多信息,请与我们联系
Here are my admin views.py这是我的管理员 views.py
@login_required
@admin_required
def profile(request):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileUpdateForm(request.POST,
request.FILES,
instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, f'Your account has been updated!')
return redirect('profile')
else:
u_form = UserUpdateForm(instance=request.user)
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'u_form': u_form,
'p_form': p_form
}
return render(request, 'users/profile.html', context)
main urls.py主要网址.py
urlpatterns = [
path('', classroom.home, name='home'),
path('students/', include(([
path('', students.QuizListView.as_view(), name='quiz_list'),
path('interests/', students.StudentInterestsView.as_view(), name='student_interests'),
path('taken/', students.TakenQuizListView.as_view(), name='taken_quiz_list'),
path('quiz/<int:pk>/', students.take_quiz, name='take_quiz'),
], 'classroom'), namespace='students')),
path('teachers/', include(([
path('', teachers.QuizListView.as_view(), name='quiz_change_list'),
path('quiz/add/', teachers.QuizCreateView.as_view(), name='quiz_add'),
path('quiz/<int:pk>/', teachers.QuizUpdateView.as_view(), name='quiz_change'),
path('quiz/<int:pk>/delete/', teachers.QuizDeleteView.as_view(), name='quiz_delete'),
path('quiz/<int:pk>/results/', teachers.QuizResultsView.as_view(), name='quiz_results'),
path('quiz/<int:pk>/question/add/', teachers.question_add, name='question_add'),
path('quiz/<int:quiz_pk>/question/<int:question_pk>/', teachers.question_change, name='question_change'),
path('quiz/<int:quiz_pk>/question/<int:question_pk>/delete/', teachers.QuestionDeleteView.as_view(), name='question_delete'),
], 'classroom'), namespace='teachers')),
path('admins/', include(([
path('', admins.profile, name='profile'),
], 'classroom'), namespace='admins')),
]
login urls.py登录网址.py
urlpatterns = [
path('', include('classroom.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/signup/', classroom.SignUpView.as_view(), name='signup'),
path('accounts/signup/student/', students.StudentSignUpView.as_view(), name='student_signup'),
path('accounts/signup/teacher/', teachers.TeacherSignUpView.as_view(), name='teacher_signup'),
path('accounts/signup/admin/', admins.register, name='register'),
]
signup form template注册表单模板
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="row">
<div class="col-md-8 col-sm-10 col-12">
<h2>Sign up as a {{ user_type }}</h2>
<form method="post" novalidate>
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
{{ form|crispy }}
<button type="submit" class="btn btn-success">Sign up</button>
</form>
</div>
</div>
{% endblock %}
admin form template管理表单模板
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<input type="hidden" name="next" value="{{ next }}">
<legend class="border-bottom mb-4">Join Today</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Sign Up</button>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">
Already Have An Account? <a class="ml-2" href="{% url 'login' %}">Sign In</a>
</small>
</div>
</div>
{% endblock content %}
Try just removing @login_required
from your profile
view.尝试从您的
profile
视图中删除@login_required
。
Since Django decorators work top-down, @login_required
will kick in before @admin_required
.由于 Django 装饰器自上而下工作,@login_required 将在
@login_required
之前@admin_required
。 But from https://pypi.org/project/django-simple-rest/ :但是来自https://pypi.org/project/django-simple-rest/ :
admin_required simply makes sure that the user is logged in and is also a super user before they will be granted access to the decorated view method.
admin_required 只是确保用户已登录并且也是超级用户,然后他们才能被授予对装饰视图方法的访问权限。
So, I think that @admin_required
will already check if the user is logged in.所以,我认为
@admin_required
已经检查用户是否登录了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.