如何解开我的项目中的Django URL?

huangapple go评论69阅读模式
英文:

How to untangle the Django URLs in my project?

问题

以下是您提供的内容的中文翻译:

我是Django的初学者。根据YouTube视频和Django文档制作了一个项目和应用程序。
分别为登录、注册和业务注册制作了HTML页面。

project/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls), #website.com/admin/
    path('', include('signin.urls')), #website.com/
    path('home/', include('signin.urls')), #website.com/home/
    path('signin/', include('signin.urls')), #website.com/signin/
    path('signup/', include('signin.urls')), #website.com/signup/
    path('business_signup/', include('signin.urls')), #website.com/business_signup/
]

signin/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.default, name='signin'), 
    path('home/', views.default, name='home'),
    path('signin/', views.default, name='signin'),
    path('signup/', views.signup, name='signup'),
    path('business_signup/', views.business_signup, name='business_signup'),
]

对于空路径、home和signin,我已重定向到登录页面。代码如下:

from django.shortcuts import render
from django.http import HttpResponse

def default(request):
    return render(request, "default.htm")

在HTML页面中:

<p>新来这里?<a href="/signup">创建一个账户</a></p>

所有的URL都可以正常工作,但问题是当我在HTML页面的href中使用{% url 'name' %}时,如下所示:

<p>新来这里?<a href="{% url 'signup' %}">创建一个账户</a></p>

所有的重定向都变成了:

http://127.0.0.1:8000/business_signup/signin/
http://127.0.0.1:8000/business_signup/signup/
http://127.0.0.1:8000/business_signup/business_signup/

我想要像这样重定向{% url 'name' %}http://127.0.0.1:8000/signin/ 等等。

有解决方案吗?

如果您需要进一步的帮助或解决方案,请随时告诉我。

英文:

I am a beginner in django. Made a project and app by following YouTube Videos and django documentation.<br>
I have made an HTML page for sign in, sign up and business sign up repectively. <br><br>
project/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path(&#39;admin/&#39;, admin.site.urls), #website.com/admin/
    path(&#39;&#39;, include(&#39;signin.urls&#39;)), #website.com/
    path(&#39;home/&#39;, include(&#39;signin.urls&#39;)), #website.com/home/
    path(&#39;signin/&#39;, include(&#39;signin.urls&#39;)), #website.com/signin/
    path(&#39;signup/&#39;, include(&#39;signin.urls&#39;)), #website.com/signup/
    path(&#39;business_signup/&#39;, include(&#39;signin.urls&#39;)), #website.com/business_signup/
]

signin/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path(&#39;&#39;, views.default, name=&#39;signin&#39;), 
    path(&#39;home/&#39;, views.default, name=&#39;home&#39;),
    path(&#39;signin/&#39;, views.default, name=&#39;signin&#39;),
    path(&#39;signup/&#39;, views.signup, name=&#39;signup&#39;),
    path(&#39;business_signup/&#39;, views.business_signup, name=&#39;business_signup&#39;),
]

I have made for empty path, home and signin redirect to sign in page. Code is like below:

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def default(request):
    return render(request, &quot;default.htm&quot;)

In the html page:

&lt;p&gt;New here? &lt;a href=&quot;/signup&quot;&gt;Create an account&lt;/a&gt;&lt;/p&gt;

All the urls are working but the problem is when i use the {% url &#39;name&#39; %} in the href of the HTML page like

&lt;p&gt;New here? &lt;a href=&quot;{% url &#39;signup&#39; %}&quot;&gt;Create an account&lt;/a&gt;&lt;/p&gt;

All the redirections are changed to

http://127.0.0.1:8000/business_signup/signin/
http://127.0.0.1:8000/business_signup/signup/
http://127.0.0.1:8000/business_signup/business_signup/

I wanted to redirect {% url &#39;name&#39; %} like http://127.0.0.1:8000/signin/ and similar.

Any solutions?

答案1

得分: 1

Here are the translated parts of your text:

"在请求 URL 时一切正常,因为在请求时 urlpatterns 会逐个匹配,解析器会首先找到模式中的第二行并使用它。

但是,如果使用带有名称的 url 标签,那么 urlpatterns 的名称将被匹配。

在服务器启动时,urlpattern 会逐个条目扫描。

如果为视图定义了名称,那么如果名称重复出现,最后一个将保留。这正是你的情况,因为你多次包含了 signin/urls.py。

在你的情况下,path('business_signup/', include('signin.urls')) 是最后一个。因此,{% url 'signup' %} 将被翻译为 ../business_signup/signup/

如果你只保留

urlpatterns = [
    path('admin/', admin.site.urls), #website.com/admin/
    path('', include('signin.urls')), #website.com/
]

一切都会正常。

顺便说一下,更合理的做法是将 /home/ 集成到基本 URL 中,而不是在 singin URLs 中:

project/urls.py

urlpatterns = [
    path('admin/', admin.site.urls), #website.com/admin/
    path('', include('signin.urls')), 
    path('home/', views.default, name='home')
]

signin/urls.py

urlpatterns = [
    path('signin/', views.default, name='signin'),
    path('signup/', views.signup, name='signup'),
    path('business_signup/', views.business_signup, name='business_signup'),
]

希望这些有帮助。"

英文:

Making a request to an url works fine, because in the moment of a request the urlpatterns are matched one-by-one and the resolver will find the second row in your pattern first and use it.

But if you use url tag with a name, then the names(!) of the urlpatterns are matched.

to do that at start of the server, the urlpattern is scanned entry by entry.

If you define names for view, then in case a name appears double, the last one is the one that will stay. This is what happens in your case because you include signin/urls.py several times.

In your case the path(&#39;business_signup/&#39;, include(&#39;signin.urls&#39;)) is the last one. So {% url &#39;signup&#39; %} will translate to ../business_signup/signup/

urlpatterns = [
    path(&#39;admin/&#39;, admin.site.urls), 
    path(&#39;&#39;, include(&#39;signin.urls&#39;)),   # this one is the first one to match if e.g. you make a request to /signin/ 
    path(&#39;home/&#39;, include(&#39;signin.urls&#39;)), 
    path(&#39;signin/&#39;, include(&#39;signin.urls&#39;)), 
    path(&#39;signup/&#39;, include(&#39;signin.urls&#39;)), 
    path(&#39;business_signup/&#39;, include(&#39;signin.urls&#39;)), # this one is the last one and therefore relevant for finally defining names
]

if you just leave

urlpatterns = [
    path(&#39;admin/&#39;, admin.site.urls), #website.com/admin/
    path(&#39;&#39;, include(&#39;signin.urls&#39;)), #website.com/
]

everything will be ok

By the way it would be more logic to integrate /home/ in the base urls and not in singin urls:

project/urls.py

urlpatterns = [
    path(&#39;admin/&#39;, admin.site.urls), #website.com/admin/
    path(&#39;&#39;, include(&#39;signin.urls&#39;)), 
    path(&#39;home/&#39;, views.default, name=&#39;home&#39;)

]

signin/urls.py

urlpatterns = [
    path(&#39;signin/&#39;, views.default, name=&#39;signin&#39;),
    path(&#39;signup/&#39;, views.signup, name=&#39;signup&#39;),
    path(&#39;business_signup/&#39;, views.business_signup, name=&#39;business_signup&#39;),
]

</details>



# 答案2
**得分**: 0

proyect/urls应该看起来像这样:
```python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('signin.urls')),
]
```

<details>
<summary>英文:</summary>

proyect/urls should look something like this:
```python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path(&#39;admin/&#39;, admin.site.urls),
    path(&#39;&#39;, include(&#39;signin.urls&#39;)),
]
```
That&#39;s it, no need to do it for every single URL.

</details>



huangapple
  • 本文由 发表于 2023年5月6日 21:02:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定