【发布时间】:2022-01-25 10:20:57
【问题描述】:
有没有办法从 app_name 获取完整的 url?像 reverse('myapp:bio') 在类 ItemForm(forms.ModelForm) 中
from django.urls import include, path
app_name = 'myapp'
urlpatterns = [
path('index/', views.index, name='main-view'),
path('bio/<username>/', views.bio, name='bio'),
]
我正在尝试在 MyForm(ModelForm) 中使用它,但我得到了那个错误
File "D:\Development\SiteDJCentral\.venv\lib\site-packages\django\urls\resolvers.py", line 607, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'website.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
print( reverse('myapp:bio') )
我尝试调用它的表单
class ItemForm(forms.ModelForm):
relase_date = forms.DateField(required=False, input_formats=settings.DATE_INPUT_FORMATS, help_text='opzionale formato yyyy-mm-dd')
tags = OdsTaggerMultipleChoiceField( queryset = None, required=True, json_url= HttpRequest.request.build_absolute_uri(reverse('myapp:bio' )) )
【问题讨论】:
-
我正在尝试在 MyForm(ModelForm) 中使用它,但我收到错误文件“D:\Development\SiteDJCentral\.venv\lib\site-packages\django\urls\resolvers.py ",第 607 行,在 url_patterns 中从 e django.core.exceptions.ImproperlyConfigured 中引发 ImproperlyConfigured(msg.format(name=self.urlconf_name)):包含的 URLconf 'website.urls' 中似乎没有任何模式。如果您在文件中看到有效模式,则问题可能是由循环导入引起的。 print(反向('items:list', args=['Orléans']))
-
您将表单添加到问题中?
-
什么是`OdsTaggerMultipleChoiceField,它来自哪里,你能展示它的代码吗?您应该避免在视图或表单类的正文中使用反向链接,在这些情况下应该使用 reverse_lazy docs.djangoproject.com/en/4.0/ref/urlresolvers/#reverse-lazy