Django重复URLS

时间:2014-05-28 02:18:27

标签: django django-urls

Django的新手。设置维基。当我需要它时,继续到下一页添加内容,它复制URL,所以我们从这里开始:

http://127.0.0.1:8000/mideastinfo/Start

我有一个舔说“没有内容,你想创造,它意味着要去这里:

http://127.0.0.1:8000/mideastinfo/Start/edit

而是这样做:

http://127.0.0.1:8000/mideastinfo/Start/mideastinfo/Start/edit

这显然不存在。 以下是urls.py

# url(r'^admin/', include(admin.site.urls)),
                   (r'^mideastinfo/(?P<page_name>[^/]+)/edit/$', 'mideastinfo.wiki.views.edit_page'),
                   (r'^mideastinfo/(?P<page_name>[^/]+)+/save/$', 'mideastinfo.wiki.views.save_page'),
                   (r'^mideastinfo/(?P<page_name>[^/]+)/$', 'mideastinfo.wiki.views.view_page')
我做错了什么?感谢您的帮助,我是一个菜鸟。

这是views.py:

# Create your views here.
from wiki.models import Page
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

def view_page(request, page_name):
try:
    page = Page.objects.get(pk=page_name)
except Page.DoesNotExist:
    return render_to_response("create.html", {"page_name":page_name}
) 
content = page.content
return render_to_response("view.html", {"page_name":page_name, "content":content})

def edit_page(request, page_name):
try:
    page = Page.objects.get(pk=page_name)
    content = page.content
except Page.DoesNotExist:
    content = ""
return render_to_response("edit.html", {"page_name":page_name, "content":content}
)

def save_page(request, page_name):
content = request.POST["content"]
try:
    page = Page.objects.get(pk=page_name)
    page.content = content
except Page.DoesNotExist:
    page = Page(name=pahe_name, conent=content)
page.save()
return HttpResponseRedirect("/mideastinfo/" + page_name + "/")

这是创建页面的模板:

<html>
<head>
    <title>{{page_name}} = Create</title>
</head>
<body>
    <h1>{{page_name}}</h1>
    This page does not exist. <a href="/{{page_name}}/edit">Create?</a>
</body>
</html>

和编辑页面:

<html>
<head>
    <title>{{page_name}} - Editing</title>
</head>
<body>
    <h1>Editing {{page_name}}</h1>
    <form method="post" action="/{{page_name}}/save/">
        <textarea name="content" rows="20" cols="60">{{content}}</textarea>         <br/>
        <input type="submit" value="Save Page"/>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在模板中使用{url 'name' param %}而不是硬代码网址,并使用reverse('name')在views.py和其他脚本中获取相应的网址:

<强> urls.py

(r'^mideastinfo/(?P<page_name>[^/]+)/edit/$', 'mideastinfo.wiki.views.edit_page',name='edit'),
(r'^mideastinfo/(?P<page_name>[^/]+)+/save/$', 'mideastinfo.wiki.views.save_page',name = 'save'),
(r'^mideastinfo/(?P<page_name>[^/]+)/$', 'mideastinfo.wiki.views.view_page',name = 'view')

用于模板

{% url 'edit' 'page_name' %}

用于views.py或其他脚本,例如models.py

reverse('view', args=('value',))

通过这种方式,你可以修复你的错误,有一天如果你想修改你的网址,只需修改urls.py即可。

更新2014 05 31

在高杠杆中,您可以在models.py的类中定义get_absolute_url()https://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url

然后您可以更轻松地重定向。

views.py依此类推

from django.shortcuts import redirect
def view2(request):
    obj = Model2.objects.get(id=33)
    return redirect(obj,permanent=True)
模板中的

使用{{ obj.get_absolute_url }}

更多请参阅:https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-resolution-of-urls

关于重定向:https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect