我有一个网址http://localhost/project_name/
。我的ListView
与url http://localhost/project_name/company/
一样运行良好。但是,当我使用CreateView
添加公司时,它会将我重定向到http://localhost/company/
,说“找不到页面”。
class CompanyListView(LoginRequiredMixin, generic.ListView):
template_name = 'company/company.html'
queryset = Company.objects.all()
context_object_name = 'companies'
paginate_by = 10
def get_queryset(self):
return (
self.queryset.exclude(id=1)
.exclude(company_is_deleted=True)
.annotate(number_of_company_users=Count('userprofile'))
)
class CompanyCreateView(LoginRequiredMixin, generic.CreateView):
model = Company
template_name = 'company/company_form.html'
fields = ['company_name', 'company_description', 'company_email',
'company_website', 'company_address', 'company_phone', 'company_status',
'company_monthly_payment', 'company_logo']
def get_success_url(self):
return reverse('superadmin_company')
project_name / urls.py
path('company/', include(company_urls)),
apps / company / urls.py
path('', views.CompanyListView.as_view(), name='superadmin_company'),
path('add', views.CompanyCreateView.as_view(), name='superadmin_company_create'),
在使用get_succes_url()
添加数据后,如何使用http://localhost/project_name/company/
重定向,并在URL中将project_name用作CreateView
答案 0 :(得分:1)
在子路径中部署应用程序时,需要在设置中定义FORCE_SCRIPT_NAME。因此,您可以像这样更新设置:
FORCE_SCRIPT_NAME = "/project_name"