Django 使用re_path 发生错误

淮南草 2019-04-16 05:24:46
无法正常显示页面

现象:
错误:
nid: 2
--- Logging error ---
--- Logging error ---
--- Logging error ---
--- Logging error ---
--- Logging error ---
--- Logging error ---
--- Logging error ---
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 51605)
nid: 2
--- Logging error ---
--- Logging error ---
--- Logging error ---
Unhandled exception in thread started by <bound method Thread._bootstrap of <Thread(Thread-3, started daemon 2500)>>
Traceback (most recent call last):
File "D:\F\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "D:\F\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\F\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\F\eclipse-workspace\day19_2\app01\views.py", line 25, in index
File "D:\F\Anaconda3\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "D:\F\Anaconda3\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "D:\F\Anaconda3\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "D:\F\Anaconda3\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "D:\F\Anaconda3\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "D:\F\Anaconda3\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "D:\F\Anaconda3\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "D:\F\Anaconda3\lib\site-packages\django\template\defaulttags.py", line 447, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "D:\F\Anaconda3\lib\site-packages\django\urls\base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "D:\F\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 636, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'indexx' with no arguments not found. 1 pattern(s) tried: ['demo/(\\d+)/']

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

源码:
urls.py

views.py

index.html

...全文
393 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
淮南草 2019-04-18
  • 打赏
  • 举报
回复
还是不行,
郑光辉 2019-04-17
  • 打赏
  • 举报
回复
path是2.0以后的新路由函数用法 demo/<int:nid>/
1、首先我们先下载DjangoUeditor包。下载完成然后解压到项目根目录里。 DjangoUeditor.zip 2、settings.py里注册APP,在INSTALLED_APPS里添加'DjangoUeditor',。 myblog/settings.y INSTALLED_APPS = [ 'django.contrib.admin', .... 'DjangoUeditor', #注册APP应用 ] 3、myblog/urls.py里添加url。 myblog/urls.py ... from django.urls import path, include #留意上面这行比原来多了一个include urlpatterns = [ path('admin/', admin.site.urls), path('', views.hello), path('ueditor/', include('DjangoUeditor.urls')), #添加DjangoUeditor的URL ] 4、修改blog/models.py里需要使用富文本编辑器渲染的字段。这里面我们要修改的是Article表里的body字段。 把原来的: blog/models.py body = models.TextField() 修改成: blog/models.py from DjangoUeditor.models import UEditorField #头部增加这行代码导入UEditorField body = UEditorField('内容', width=800, height=500, toolbars="full", imagePath="upimg/", filePath="upfile/", upload_settings={"imageMaxSize": 1204000}, settings={}, command=None, blank=True ) 留意里面的imagePath="upimg/", filePath="upfile/" 这两个是图片和文件上传的路径,我们上传文件,会自动上传到项目根目录media文件夹下对应的upimg和upfile目录里,这个目录名可以自行定义。有的人问,为什么会上传到media目录里去呢?那是因为之前我们在基础配置文章里,设置了上传文件目录media。 上面步骤完成后,我们启动项目,进入文章发布页面。提示出错: render() got an unexpected keyword argument 'renderer' 错误页面上有提示,出错的地方是下面文件的93行。 F:\course\myblog\myblogvenv\lib\site-packages\django\forms\boundfield.py in as_widget, line 93 我这里使用的是最新版本的Django2.1.1所以报错,解决办法很简单。打开这个文件的93行,注释这行即可。 修改成之后,重新刷新页面,就可以看到我们的富文本编辑器正常显示。 留意,如果我们在富文本编辑器里,上传图片,在编辑器内容里不显示上传的图片。那我们还需要进行如下设置,打开myblog/urls.py文件,在里面输入如下代码: myblog/urls.py .... from django.urls import path, include, re_path #上面这行多加了一个re_path from django.views.static import serve #导入静态文件模块 from django.conf import settings #导入配置文件里的文件上传配置 urlpatterns = [ path('admin/', admin.site.urls), .... re_path('^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),#增加此行 ] 设置好了之后,图片就会正常显示。这样我们就可以用DjangoUeditor富文本编辑器发布图文并茂的文章了。

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧