【问题标题】:can not access webpage django无法访问网页django
【发布时间】:2022-07-16 22:25:08
【问题描述】:

我正在处理 django 上的用户页面。我想输入 http://127.0.0.1:8000/user/2 并获取我的模板,但 django 说 The current path, user/2, didn’t match any of these.

我的看法:

from django.shortcuts import render
from django.views import generic
from .models import *

def UserPage(request,user_num):
    #get user info
    username = "none"
    posts = []#post("test", "none")
    status = 'aboba'
    return render(
        request,
        'userpage.html',
        context={'username': username, 'posts': posts, 'status': status},
    )

我的网址:

from django.urls import path
from . import views


urlpatterns = [
    path(r'^user/(?P<user_num>\d+)$', views.UserPage, name='userpage'),
]

【问题讨论】:

    标签: python django


    【解决方案1】:

    我相信这是因为您将regex path 和常规path 混合在一起,要么摆脱正则表达式的东西,要么使用re_path

     path('user/<user_num>'
    
    from django.urls import re_path
    re_path(r'^user/(?P<user_num>\d+)$'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      • 2018-04-14
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多