- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Pillow 在我的网络应用程序中添加用户可上传的图像。我创建了一个 Django Upload 模型并将其注册到 Admin 中。当我使用管理控制台添加照片后,我收到以下错误。最初该网站运行良好
错误
NoReverseMatch at /
Reverse for 'thing_detail' with arguments '()' and keyword arguments '{u'slug': ''}' not found. 1 pattern(s) tried: ['things/(?P<slug>[-\\w]+)/$']
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.8.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'thing_detail' with arguments '()' and keyword arguments '{u'slug': ''}' not found. 1 pattern(s) tried: ['things/(?P<slug>[-\\w]+)/$']
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 496
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/shashank/development/hellowebapp/hellowebapp',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time: Sun, 13 Mar 2016 18:54:31 +0000
网址.py
from collection.backends import MyRegistrationView
from django.conf.urls import include, url
from django.contrib import admin
from collection import views
from django.conf import settings
from django.views.generic import TemplateView
from django.contrib.auth.views import (
password_reset,
password_reset_done,
password_reset_confirm,
password_reset_complete
)
urlpatterns = [
url(r'^$', views.index, name='home'),
url(r'^about/$',TemplateView.as_view(template_name='about.html'),name='about'),
url(r'^contact/$',TemplateView.as_view(template_name='contact.html'),name='contact'),
url(r'^things/(?P<slug>[-\w]+)/$','collection.views.thing_detail',name='thing_detail'),
url(r'^things/(?P<slug>[-\w]+)/edit/$','collection.views.edit_thing',name='edit_thing'),
url(r'^things/(?P<slug>[-\w]+)/edit/weight$','collection.views.edit_weight',name='edit_weight'),
url(r'^things/(?P<slug>[-\w]+)/delete/weight$','collection.views.remove_weight',name='remove_weight'),
#WORKING url(r'^things/(?P<pk>\d+)/remove/$', 'collection.views.remove_weight', name='remove_weight'),
url(r'^things/$',TemplateView.as_view(template_name='weight_removed.html'),name='weight_removed'),
url(r'^(?P<slug>[\w\d-]+)/(?P<pk>\d+)/$','collection.views.remove_weight',name='remove_weight'),
#url(r'^edit/(?P<slug>\d+)/weights$', 'collection.views.AddWeight',name='AddWeight'),
# the new password reset URLs
url(r'^accounts/password/reset/$',password_reset,{'template_name':'registration/password_reset_form.html'},name="password_reset"),
url(r'^accounts/password/reset/done/$',password_reset_done,{'template_name':'registration/password_reset_done.html'},name="password_reset_done"),
url(r'^accounts/password/reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',password_reset_confirm,{'template_name':'registration/password_reset_confirm.html'},name="password_reset_confirm"),
url(r'^accounts/password/done/$',password_reset_complete,{'template_name':'registration/password_reset_complete.html'},name="password_reset_complete"),
#setup additional registeration page
url(r'^accounts/register/$',MyRegistrationView.as_view(),name='registration_register'),
url(r'^accounts/create_thing/$','collection.views.create_thing',name='registration_create_thing'),
url(r'^accounts/',include('registration.backends.default.urls')),
url(r'^admin/', include(admin.site.urls)),
]
if settings.DEBUG:
urlpatterns += [
url(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT,}),
]
模型.py
from django.db import models
from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone
class Thing(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
slug = models.SlugField(unique=True)
user = models.OneToOneField(User, blank=True, null=True)
class Weight(models.Model):
date = models.DateTimeField(default=timezone.now)
weight_value = models.CharField(max_length=255)
thingRA = models.ForeignKey(Thing,related_name="weights")
class Meta:
order_with_respect_to = 'thingRA'
ordering = ['date']
def get_image_path(instance, filename):
return '/'.join(['thing_images', instance.thing.slug, filename])
class Upload(models.Model):
thing = models.ForeignKey(Thing, related_name="uploads")
image = models.ImageField(upload_to=get_image_path)
管理.py
from django.contrib import admin
# import your model
from collection.models import Thing, Weight, Upload
class ThingAdmin(admin.ModelAdmin):
model = Thing
list_display = ('name', 'description',)
prepopulated_fields = {'slug': ('name',)}
# and register it
admin.site.register(Thing, ThingAdmin)
class WeightAdmin(admin.ModelAdmin):
model = Weight
list_display = ('date','weight_value',)
admin.site.register(Weight, WeightAdmin)
class UploadAdmin(admin.ModelAdmin):
list_display = ('thing', )
list_display_links = ('thing',)
# and register it
admin.site.register(Upload, UploadAdmin)
Base.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>
{% block title %}
WEB PAGE BY SHASHANK
{% endblock title %}
</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}" />
</head>
<body>
<div id="page">
<div id="logo">
<h1><a href="/" id="logoLink">S PORTAL</a></h1>
</div>
<div id="nav">
<ul>
<li><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'about' %}">About</a></li>
<li><a href="{% url 'contact' %}">Contact</a></li>
{% if user.is_authenticated %}
<li><a href="{% url 'auth_logout' %}">Logout</a></li>
<li><a href="{% url 'thing_detail' slug=user.thing.slug %}">My Profile</a></li>
{% else %}
<li><a href="{% url 'auth_login' %}">Login</a></li>
<li><a href="{% url 'registration_register' %}">Register</a></li>
{% endif %}
</ul>
</div>
{% block content %}{% endblock content %}
<div id="footer">
<p>
Webpage made by <a href="/" target="_blank">SHASHANK</a>
</p>
</div>
</div>
</body>
</html>
查看.py
from django.shortcuts import render,redirect,get_object_or_404
from collection.models import Thing, Weight
from collection.forms import ThingForm, WeightForm, ThingWeightFormSet
from django.template.defaultfilters import slugify
from django.contrib.auth.decorators import login_required
from django.http import Http404
from django.views.decorators.csrf import csrf_protect
from django.views.generic import ListView, CreateView, UpdateView
from django import forms
def index(request):
things = Thing.objects.all()
return render(request,'index.html',{'things':things,})
def thing_detail(request, slug):
# grab the object...
thingRA = Thing.objects.get(slug=slug)
weights = thingRA.weights.all().order_by('-date')
# and pass to the template
return render(request, 'things/thing_detail.html', {'thing': thingRA, 'weights':weights,})
def edit_thing(request, slug):
# grab the object
thing = Thing.objects.get(slug=slug)
# set the form we're using
form_class = ThingForm
# if we're coming to this view from a submitted form
if request.method == 'POST':
# grab the data from the submitted form and apply to
# the form
form = form_class(data=request.POST, instance=thing)
if form.is_valid():
# save the new data
form.save()
return redirect('thing_detail', slug=thing.slug)
# otherwise just create the form
else:
form = form_class(instance=thing)
# and render the template
return render(request, 'things/edit_thing.html', {'thing': thing,'form': form,})
def create_thing(request):
form_class = ThingForm
if request.method == 'POST':
form = form_class(request.POST)
if form.is_valid():
thing = form.save(commit=False)
thing.user = request.user
thing.slug = slugify(thing.name)
thing.save()
slug = slugify(thing.name)
return redirect('thing_detail', slug=thing.slug)
else:
form = form_class()
return render(request,'things/create_thing.html', {'form': form,})
def edit_weight(request, slug):
thing = get_object_or_404(Thing, slug=slug)
if request.method == "POST":
form = WeightForm(request.POST)
if form.is_valid():
weight = form.save(commit=False)
weight.thingRA = thing
weight.save()
return redirect('thing_detail', slug=thing.slug)
else:
form = WeightForm()
return render(request, 'things/edit_weight.html', {'form': form})
"""WORKING WEIGHT
def remove_weight(request, pk):
weight = get_object_or_404(Weight, pk=pk)
thing_pk = weight.thingRA.pk
weight.delete()
return redirect('weight_removed')
"""
def remove_weight(request, pk, slug):
weight = get_object_or_404(Weight, pk=pk)
thing = get_object_or_404(Thing, slug=slug)
thing_pk = weight.thingRA.pk
weight.delete()
return redirect('thing_detail', slug=slug)
@login_required
def edit_thing(request, slug):
# grab the object...
thing = Thing.objects.get(slug=slug)
# make sure the logged in user is the owner of the thing
if thing.user != request.user:
raise Http404
# set the form we're using...
form_class = ThingForm
# if we're coming to this view from a submitted form,
if request.method == 'POST':
# grab the data from the submitted form and
# apply to the form
form = form_class(data=request.POST, instance=thing)
if form.is_valid():
# save the new data
form.save()
return redirect('thing_detail', slug=thing.slug)
# otherwise just create the form
else:
form = form_class(instance=thing)
# and render the template
return render(request, 'things/edit_thing.html', {'thing': thing,'form': form,})
最佳答案
我想说你有一个带有 slug=""
的东西。您可以使用 django shell 进行检查:
from yourapp.models import Thing
Thing.objects.get(slug='')
根据您的模型定义,slug
可以为空,但您的 url 模式不接受空白 slug。您必须修复您的 slug 字段或 url 模式。
关于python - Django NoReverseMatch 位于/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35974432/
我正在尝试添加我单独制作的用户应用程序,然后尝试将其添加到我的博客中。但问题是每次我尝试添加这个东西都会搞砸,就像我现在遇到的这个错误一样。 raise NoReverseMatch(msg) dja
我有以下设置: /landing_pages views.py urls.py 在urls.py中,当我尝试访问/competition时,我有以下内容: from django.conf.u
我在浏览器中遇到此异常,我已经看到多达 20 篇与此错误相关的帖子,但我找不到任何解决方案。我是 Django 的新手,请帮助我,在此先感谢。 我的项目名/urls.py urlpatterns =
我有这个问题,被困了几个小时。 NoReverseMatch位于/ 找不到参数“('',)”的“hotovo”。尝试了1个模式:['hotovo/(?P [^/] +)$']。 网址: urlpatt
所以我做了很多博客教程中的一个,现在我正在开发自己的应用程序。我遇到了这个错误: Reverse for '' with arguments '()' and keyword arguments '{
使用 Django 1.10.4。我有一个模型 Stream,我为其创建了 CreateView。当通过管理面板创建对象时,一切正常,但是当我使用 CreateView 表单时,创建了一个对象(在管理
为什么我会得到 Reverse for 'explorer_js' not found. 'explorer_js' is not a valid view function or pattern n
我正在构建一个函数,以在用户单击链接时增加模型,但即使我传递了所有参数,我仍然无法解决此问题。 Reverse for 'karma' with keyword arguments '{'token'
我正在尝试向 View 发送 AJAX 发布请求,但由于某种原因我收到了 Http500 错误。 这是 AJAX 函数: function update_coins() { $.ajax({
我不明白这是怎么回事?我试图制作一个项目的详细信息页面,例如 http://tutorial.djangogirls.org/en/extend_your_application/index.html
我正在 django 1.6(和 python 2.7)中制作一个简单的登录应用程序,但我在开始时遇到错误,无法继续。 这是网站的 url.py from django.conf.urls impor
调试了一段时间后,我发现了错误是什么,但我不知道如何修复它。 我有一个名为“ver_caja”的 urlConf ' 谁接收一个 caja 对象的 id 作为参数,然后调用泛型 object_deta
刚开始使用 Django,但遇到了一些困难——我决定尝试编写一个简单的博客引擎,同时引用 django-basic-apps 库。 在 blog/urls.py 中,我有这个条目按日期映射到实际的帖子
我正在开发一个简单的应用程序,宠物用户可以在其中创建关于他们宠物的板并在板上显示图片。 我正在尝试创建一个功能,用户可以点击他们的图板,将他们重定向到他们的图板,该图板将显示他们所有的宠物图片。 当我
我在 View 中有两种方法 create 和 update ,其中 update 接受一个参数,而 create 不接受任何参数。我决定将它们变成只有一个函数 update_create 因为它们没
我遇到了这个错误,但似乎无法弄清楚。我直接从以前的 Django 项目复制它,因此造成了部分困惑。 TemplateSyntaxError at Caught NoReverseMatch while
我在让 password_Reset_confirm 位工作时遇到问题。 网址: (r'^password_reset/$', 'django.contrib.auth.views.password_
目前 Django URL 标记出现 NoReverseMatch 错误。一直在关注 Django 权威指南、Django 文档,并在此处和互联网上进行搜索 网址: url(r'^test/', Se
我又花了一个晚上在这上面...有人可以帮忙吗? 我正在尝试在 http://lightbird.net/dbe/todo_list.html 复制 kickstart 示例这似乎是为早期版本的 Dja
我在尝试使用 url 标记链接到 View 时遇到了这个错误。错误发生在这一行: {% for algorithim in algorithims %} 在模板中。 不太确定我哪里出错了。我想我已经附
我是一名优秀的程序员,十分优秀!