简体   繁体   English

django中的内部url重定向

[英]internal url redirection in django

I'm fairly new to Django(experienced in Python though). 我是Django的新手(虽然有Python经验)。 I'm trying to make a new website using it for the first time and this problem is bothering me. 我正在尝试使用它来建立一个新的网站,这个问题困扰着我。

Here's the user flow: 这是用户流程:

  1. The user goes to my site www.newco.com 用户转到我的网站www.newco.com
  2. He is greeted by the homepage and links for 'login','signup' ,'about' etc. 他受到主页的欢迎,并带有“登录”,“注册”,“关于”等链接。

  3. The user clicks on those links and goes to the respective pages. 用户单击那些链接,然后转到相应的页面。

Here's my root urlconf: 这是我的根urlconf:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^$',include('homepage.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^login/$', include('userlogin.urls')),
    url(r'^signup/$', include('userlogin.urls'))

]

Here's my homepage.urls: 这是我的homepage.urls:

from django.conf.urls import url
from . import views

urlpatterns=[
  url(r'^$',views.index,name='index'),
  url(r'^about$',views.about,name='about'),

]

and finally here's my homepage.views file: 最后是我的homepage.views文件:

from django.shortcuts import render

def index(request):

    return render(
        request,
        'index.html'

    )



def about(request):
    return render(
        request,
        'about.html'


     )

When I go to my development server's homepage, the homepage renders correctly. 当我转到开发服务器的主页时,该主页将正确呈现。 I tried two approaches for going to the 'about us' page: 我尝试了两种方法进入“关于我们”页面:

  1. I hardcoded the URL as <a href="/about"> About us </a> . 我将该网址硬编码为<a href="/about"> About us </a> That redirected me to the homepage. 那把我重定向到了主页。

  2. I coded the URL as {% url "about" %} . 我将该网址编码为{% url "about" %} This gives me the error: Reverse for 'about' with arguments '()' and keyword arguments '{}' not found. 这给了我错误: 找不到带有参数'()'和关键字参数'{}'的'about'。 1 pattern(s) tried: ['$about$'] . 尝试了1个模式:['$ about $']

How should I proceed about this problem, since this is a specific instance of a larger problem. 因为这是一个较大问题的特定实例,所以我应该如何处理该问题。 Thanks for reading. 谢谢阅读。

You get this error because you have defined like this: 您收到此错误,因为您已定义如下:

url(r'^$',include('homepage.urls')),

and it should be: 它应该是:

url(r'^',include('homepage.urls')),

When you use $ you are telling that the regex ends there. 当您使用$您会说正则表达式到此结束。 That should fix your problem. 那应该解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM