Skip to main content
 首页 » 编程设计

python之使用 Django 找不到页面

2025年05月04日23bonelee

我已经使用我创建的模板文件夹中的 index.html 文件设置了我的项目,它在 http://example.com 上运行良好。 .当我将 welcome.html 文件添加到模板文件夹并设置 urls.py 和 views.py 文件时,我在 http://example.com/welcome.html 收到页面未找到消息。 .我已经阅读了一些引用站点来进行故障排除,但仍然无法通过 404 浏览器页面...

在 urls.py 中:

from django.conf.urls import patterns,include, url 
from django.contrib import admin 
from Payments.create import views 
 
 
urlpatterns = patterns('Payments.create.views', 
url(r'^$', 'home', name='home'), 
url(r'^/welcome.html$', 'authorization', name='authorization'), 
) 

在 views.py 中:

def authorization(request): 
    WPdata = request.GET.get('data') 
    if WPdata and len(WPdata) > 0: 
        content = {'title' : 'My First Post', 
            'author' : WPdata, 
            'date' : '18th September', 
            'body' : ' Lorem ipsum' 
        } 
    else: 
        content = "" 
 
    return render(request,'welcome.html',content)  
 
def home(request): 
    if request.method == 'POST': 
        form = NameForm(request.POST) 
        if form.is_valid(): 
            connection = http.client.HTTPSConnection('api.parse.com', 443) 
            connection.connect() 
            connection.request('POST', '/1/classes/TestObject', json.dumps({ 
            "foo": form.cleaned_data['testCode'] 
            }), { 
                 "X-Parse-Application-Id": "xx", 
                 "X-Parse-REST-API-Key": "xx", 
                 "Content-Type": "application/json" 
        }) 
        results = json.loads(connection.getresponse().read()) 
    else: 
        form = NameForm() 
 
    return render(request,'index.html',{})    

我不确定按预期工作的 index.html 设置和不按预期工作的 welcome.html 设置之间有什么区别...我尝试了多种不同的 url 模式排列,但没有任何效果被认可,因此将不胜感激提供的任何帮助!

请您参考如下方法:

为什么不只使用 welcome 而不是 welcome.html?不管怎样,试试这个:

url(r'^welcome.html$', 'authorization', name='authorization'), 

请注意,我已经删除了第一个 '/'