reverse
在视图中不返回url名称空间
class UserCreateView(CreateView):
model = User
# form_class = UserCreationForm
template_name = "users/create_user.html"
success_url= "/users/create"
def get_form_class(self):
if self.request.user.user_level == 1 or self.request.user.user_level == 10:
form_class = UserCreationForm
return form_class
elif self.request.user.user_level == 20:
form_class = UserCreationFormCompanyManager
return form_class
else:
form_class = UserCreationFormHomeManager
return form_class
def post(self, request, *args, **kwargs):
form = self.get_form_class()
user_level = User.objects.get(id=request.user.id)
# form = UserCreationFormHomeManager(request.POST)
if request.method=='POST':
form = UserCreationFormCompanyManager(request.POST)
if user_level == 20:
if form.is_valid():
obj = form.save(commit=False)
obj.company = self.request.user.company
obj.password = make_password(form.cleaned_data['password'])
obj.save()
return obj
elif user_level == 30:
if request.method=='POST':
form = UserCreationFormHomeManager(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.company = self.request.user.company
obj.home = self.request.user.home
obj.password = make_password(form.cleaned_data['password'])
obj.save()
return obj
else:
if request.method=='POST':
form = UserCreationFormHomeManager(request.POST)
if form.is_valid():
obj.password = make_password(form.cleaned_data['password'])
obj = form.save()
return obj
return reverse('create-user')`
user_create_view = UserCreateView.as_view()
urls.py
from django.urls import path
from sharefile.users.views import (
user_list_view,
user_redirect_view,
user_update_view,
user_detail_view,
user_create_view,
home_create_view,
company_create_view,
)
app_name = "users"
urlpatterns = [
path("", view=user_list_view, name="list"),
path("~redirect/", view=user_redirect_view, name="redirect"),
path("~update/", view=user_update_view, name="update"),
path("user/<str:username>/", view=user_detail_view, name="detail"),
path("create/", view=user_create_view, name="create-user"),
path("create-home/", view=home_create_view, name="create-home"),
path("create-company/", view=company_create_view, name="create-company"),
# path("create-group/", view=group_create_view, name="create-group"),
# path("create-permission/", view=permission_create_view, name="create-permission"),
]
追踪:
traceback (most recent call last):
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python3.6/contextlib.py", line 52, in inner
return func(*args, **kwds)
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/views/generic/base.py", line 89, in dispatch
return handler(request, *args, **kwargs)
File "/home/bishwa/PycharmProjects/sharefile/sharefile/users/views.py", line 67, in post
return reverse('create-user')
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/urls/base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/urls/resolvers.py", line 634, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'create-user' not found. 'create-user' is not a valid view function or pattern name.
[11/Jan/2019 15:52:19] "POST /users/create/ HTTP/1.1" 500 117938