在这篇文章中,我们将带领您了解Djangourls.py+React路由器的全貌,包括react路由表的相关情况。同时,我们还将为您介绍有关angularroute与djangourls冲突怎么解决?
在这篇文章中,我们将带领您了解Django urls.py + React 路由器的全貌,包括react路由表的相关情况。同时,我们还将为您介绍有关angular route 与 django urls 冲突怎么解决?、Django 4.0 from django.conf.urls import url、Django HTML中的URL变量和view.py urls.py的关系、Django URL 错误:website.urls 中定义的 URLconf,Django 尝试了这些 URL 模式的知识,以帮助您更好地理解这个主题。
本文目录一览:- Django urls.py + React 路由器(react路由表)
- angular route 与 django urls 冲突怎么解决?
- Django 4.0 from django.conf.urls import url
- Django HTML中的URL变量和view.py urls.py的关系
- Django URL 错误:website.urls 中定义的 URLconf,Django 尝试了这些 URL 模式
Django urls.py + React 路由器(react路由表)
如何解决Django urls.py + React 路由器
我构建了一个 Django API 和一个 React 前端来使用 API。我正在使用 react-router 并且可以很好地导航,直到我刷新页面。然后我从 Django 中得到一个 Not Found
错误。
我不确定如何使用 Django 设置这些路径,以便它知道要提供什么服务。
api/urls.py
urlpatterns = [
path('''',views.apiOverview,name=''api-overview''),path(''product-list'',views.product_list,name=''product-list''),path(''product/<str:slug>'',views.product_detail,name=''product-detail''),path(''images/<str:fk>'',views.product_images,name=''product_images''),]
urls.py
urlpatterns = [
path('''',TemplateView.as_view(template_name="index.html"),name=''app''),path(''admin/'',admin.site.urls),path(''api/'',include(''api.urls'')),] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
app.js
function App() {
return (
<Router>
<Nav />
<div className="App">
<Switch>
<Route path="/products/:slug" exact component={ ProductDetail } />
<Route path="/products">
<Projects />
</Route>
<Route path="/">
<div className=''container''>
<Header />
<hr />
<h2 className="section-title">Featured Products</h2>
<ProductsListHome />
</div>
</Route>
</Switch>
</div>
<Footer />
</Router>
);
}
解决方法
如果您的应用部署到 Netlify,请将名为 _redirects
的文件添加到您的公共文件夹。然后,添加以下行:
/* /index.html 200
angular route 与 django urls 冲突怎么解决?
app.js
var app = angular.module(''app'', [
''ngResource'',
''ngRoute'',
// ''ui.bootstrap'',
// ''ngResource'',
''student'',
]);
app.config(
function(
$locationProvider,
$routeProvider
){
$locationProvider.html5Mode({
enabled:true
})
$routeProvider.
when("/", {
template: ''base'',
}).
when("/student/1", {
template: "<student-detail></student-detail>",
}).
otherwise({
template: "Not Found"
})
});
student.js
var app = angular.module(''student'', []);
app.component(''studentDetail'',{
templateUrl:''studentDetail.html'',
controller: function($scope) {
$scope.test = ''Got it.''
}
});
urls.py
class SimpleStaticView(TemplateView):
def get_template_names(self):
return [self.kwargs.get(''template_name'') + ".html"]
urlpatterns = [
url(r''^admin/'', include(admin.site.urls)),
url(r''^api/'', include("students.api.urls", namespace=''students-api'')),
url(r''^(?P<template_name>\w+)$'', SimpleStaticView.as_view(), name=''example''),
url(r''^$'', TemplateView.as_view(template_name=''home.html'')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
测试,当访问/
,base
字段是出现的,说明ng-view
工作 正常,但当访问/students/1
时,返回django路由报错,未找到该路由。
studentDetail.html
是存在的。
这是angular没获取到路由请求吗?该如何解决?谢谢。
angular route 与 django urls 冲突怎么解决? >> python
这个答案描述的挺清楚的:
http://www.goodpm.net/postreply/python/1010000008991595/angularroute与djangourls冲突怎么解决.html
Django 4.0 from django.conf.urls import url
from django.conf.urls import url 不能使用,无法使用
Cannot find reference 'url' in '__init__.py'
url
已在Django 4.0中删除。请查看此处的发行说明:
https://docs.djangoproject.com/pl/4.0/releases/4.0/#features-removed-in-4-0
django.conf.urls.url()
is removed.
@H_301_26@解决方法;
使用 re_path
替代 url
The easiest fix is to replace url()
with re_path()
. re_path
uses regexes like url
, so you only have to update the import and replace url
with re_path
.
from django.urls import include, re_path
from myapp.views import home
urlpatterns = [
re_path(r'^$', home, name='home'),
re_path(r'^myapp/', include('myapp.urls'),
]
Alternatively, you Could switch to using path
. path()
does not use regexes, so you'll have to update your URL patterns if you switch to path.
from django.urls import include, path
from myapp.views import home
urlpatterns = [
path('', home, name='home'),
path('myapp/', include('myapp.urls'),
]
If you have a large project with many URL patterns to update, you may find the django-upgrade library useful to update your urls.py
files.
REF
https://forum.djangoproject.com/t/django-4-0-url-import-error/11065/3
https://stackoverflow.com/questions/70319606/importerror-cannot-import-name-url-from-django-conf-urls-after-upgrading-to
Django HTML中的URL变量和view.py urls.py的关系
url()函数看起来的格式象:
url(r^/account/$’, views.index, name=index),
它可以接收四个参数,分别是两个必选参数:regex、view和两个可选参数:kwargs、name,接下来详细介绍这四个参数。
regex
regex代表一个正则表达式,凡是与regex匹配的URL请求都会执行到url()函数中对应的第二个参数view代表的视图函数中。需要注意的是:正则表达式不会匹配URL中的域名和查询参数,
如:
http://www.foofish.net/article/?page=3, Django只找article/。
正则表达式在URLconf模块加载时就编译好了,所以在匹配的时候速度是很快的。
view
Django匹配正则表达式成功后,就会找到相应的视图函数,Django始终用HttpRequest对象作为第一个参数传递给视图函数,此外使用regex参数中携带的参数作为可选参数传递给视图函数。
如:
url(r’^(?P<article_id>d+)/$’, views.detail, name=’detail’),
括号对
(?P<article_id>d+)
里面的参数将作为第二个参数传递给视图函数
detail(request, article_id)
这里参数的名字必须一模一样。因为你在url函数中显示的指定了该参数的名字,当然你也可以不显示的指定,
如:
url(r’^(d+)/$’, views.detail, name=’detail’)
这样在视图函数里,第二个参数的名称就随便命名了。它根据位置参数的位置来匹配。
name
讲name之前,先说说Django template的内建标签url,{% url path.to.some_view%}可以返回视图函数对应的URL(相对域名的绝对路径),比如url(r^/account/$’, views.index, name=index),使用{% url view.index %}将返回/accout/,这样做可以提高模版的灵活性,如果是使用硬编码的方式,模版难以维护。
使用标签url的时候可能会遇到一个问题就是:对于:
urlpatterns = patterns('''',
url(r''^archive/(d{4})/$'', archive, name="full-archive"),
url(r''^archive-summary/(d{4})/$'', archive, {''summary'': True}, "arch-summary"),
)
同一个视图函数有多个urlconf,此时模版系统想通过视图名archive获取URL时,就不知所措了,name参数就是用来解决此问题的。name用来唯一区一个视图对应多个urlconf的场景。通过name来反向获取URL。
如:
urlpatterns = patterns('''',
url(r''^archive/(d{4})/$'', archive, name="full-archive"),
url(r''^archive-summary/(d{4})/$'', archive, {''summary'': True}, "arch-summary"),
)
在模版中可以使用:
{% url arch-summary 1945 %}
{% url full-archive 2007 %}
kwargs
kwargs就是一个字典类型的参数,它的使用方式如:
url(r''^archive-summary/(d{4})/$'', archive, {''summary'': True}, "arch-summary"),
这里的kwargs 就是
{''summary'': True}
视图函数中就是这样使用:
def archive(request, archive_id, summary):
注意:
如果在url.py中有
url(r’^comment/(d{1,9})/delete/$’,''delete_comment’)
的配置,如果不存在delete_comment这样一个函数视图,如果在模版中使用了
{% url path.to.some_view %}
这个标签,那么抛出 ViewDoesNotExit错误。仔细想想很有道理,如果视图不存在,即使匹配到了URL,当访问这个URL的时候,还是会抛ViewDoesNotExit的异常,这里Django只是在加载解析URLConf的时候就做了检查。
如果在根url.py文件中使用了
url(r’^people/’, include(‘people.urls’, namespace=’people’))
这里people是一个app,那么在people这个app中的url.py中
url(r’^(d{1,9})/$’,''index’, name=’index’)
必须指定了name=index才能正常使用
{% url ‘people:index’%}
,否则:
NoReverseMatch at /
Reverse for ''subjects'' with arguments ''()'' and keyword arguments ''{}'' not found
当然如果你确定不是上述问题抛出的此异常,那么可以看下这两个答案:
http://stackoverflow.com/questions/9649587/reverse-for-with-arguments-and-keyword-arguments-not-found
http://stackoverflow.com/questions/14882491/django-release-1-5-url-requires-a-non-empty-first-argument-the-syntax-change
本文参考
https://docs.djangoproject.com/en/1.1/topics/http/urls/#id2
https://docs.djangoproject.com/en/1.1/ref/templates/builtins/#std:templatetag-url
Django URL 错误:website.urls 中定义的 URLconf,Django 尝试了这些 URL 模式
如何解决Django URL 错误:website.urls 中定义的 URLconf,Django 尝试了这些 URL 模式
我正在我自己的域上使用 Nginx/gunicorn 在 ubuntu 数字海洋水滴上运行 django 项目。我正在使用 Django 3.1.6 版和 Python 3.8.5 的虚拟环境
我正在尝试按照 Corey Shafers Django tutorial 创建一个博客应用程序,虽然我可以访问 example.com 上的通用 django 成功页面,但我从 example.com/blog 遇到了以下内容:
找不到页面 (404) 请求方式:GET 请求网址:example.com/blog 使用在 website.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:
管理员/ 当前路径,博客,与这些都不匹配。
我当前的 ~/projectdir/website/urls.py(见底部编辑):
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path(''admin/'',admin.site.urls),path('''',include(''blog.urls'')),]
我的 ~/projectdir/blog/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('''',views.home,name=''blog-home''),path(''about/'',views.about,name=''blog-about''),]