python - Reverse for 'detail' with keyword arguments '{'id': 5}' not found. 1 pattern(s) tried: ['posts/(?P<slug>[\\w-]+)/$'] -
i'm super stuck noreversematch @ /posts/ error. maybe overlooking small or maybe issue bigger digging.
http://imgur.com/nlymi9v error message when models.py this:
def get_absolute_url(self): #return reverse("detail", kwargs={ "slug": self.slug }) return reverse("posts:detail", kwargs={ "slug": self.slug })
http://imgur.com/a/rilrb error message when models.py this:
def get_absolute_url(self): return reverse("detail", kwargs={ "slug": self.slug }) #return reverse("posts:detail", kwargs={ "slug": self.slug })
models.py
#from _future_ import __unicode__literals django.db import models django.core.urlresolvers import reverse django.db.models.signals import pre_save django.utils.text import slugify # create models here. # mvc models views controller def upload_location(instance, filename): # filebase, extension = filename.spilt(".") # return "%s/%s.%s" %(instance.id, instance.id, filename) return "%s/%s" %(instance.id, filename) class post(models.model): title = models.charfield(max_length=120) slug = models.slugfield(unique=true) image = models.imagefield(upload_to=upload_location, null=true, blank=true, width_field="width_field", height_field="height_field") height_field = models.integerfield(default=0) width_field = models.integerfield(default=0) content = models.textfield() updated = models.datetimefield(auto_now=true, auto_now_add=false) timestamp = models.datetimefield(auto_now=false, auto_now_add=true) def __unicode__(self): return self.title def __str__(self): return self.title def get_absolute_url(self): #return reverse("detail", kwargs={ "slug": self.slug }) return reverse("posts:detail", kwargs={ "slug": self.slug }) class meta: ordering = ["-timestamp", "-updated"] def create_slug(instance, new_slug=none): slug = slugify(instance.title) if new_slug not none: slug = new_slug qs = post.objects.filter(slug=slug).order_by("-id") exists = qs.exists() if exists: new_slug = "%s-%s" %(slug, qs.first().id) return create_slug(instance, new_slug=new_slug) return slug def pre_save_post_receiver(sender, instance, *args, **kwargs): if not instance.slug: instance.slug = create_slug(instance) pre_save.connect(pre_save_post_receiver, sender=post)
views.py
from django.contrib import messages django.core.paginator import paginator, emptypage, pagenotaninteger django.http import httpresponse, httpresponseredirect django.shortcuts import render, get_object_or_404, redirect .forms import postform # create views here. .models import post def post_create(request): form = postform(request.post or none, request.files or none,) if form.is_valid(): instance = form.save(commit=false) instance.save() # message success messages.success(request, "successfully created!") return httpresponseredirect(instance.get_absolute_url()) context = { "form": form, } return render(request, "post_form.html", context) def post_detail(request, slug=none): #instant = post.objects.get(id=0) instance = get_object_or_404(post, slug=slug) context = { "title": instance.title, "instance": instance, } return render(request, "post_detail.html", context) def post_list(request): queryset_list = post.objects.all() #.order_by("-timestamp") paginator = paginator(queryset_list, 5) # show 5 contacts per page page_request_var = "page" page = request.get.get(page_request_var) try: queryset = paginator.page(page) except pagenotaninteger: # if page not integer, deliver first page. queryset = paginator.page(1) except emptypage: # if page out of range (e.g. 9999), deliver last page of results. queryset = paginator.page(paginator.num_pages) context = { "object_list": queryset, "title": "list", "page_request_var": page_request_var } return render(request, "post_list.html", context) #return httpresponse("<h1>list</h1>") def post_update(request, slug=none): instance = get_object_or_404(post, slug=slug) form = postform(request.post or none, request.files or none, instance=instance) if form.is_valid(): instance = form.save(commit=false) instance.save() # message success messages.success(request, "<a href='#'>item</a> saved", extra_tags='html_safe') return httpresponseredirect(instance.get_absolute_url()) context = { "title": instance.title, "instance": instance, "form":form, } return render(request, "post_form.html", context) def post_delete(request, slug=none): instance = get_object_or_404(post, slug=slug) instance.delete() messages.success(request, "successfully deleted!") return redirect("posts:list")
urls.py
from django.conf.urls import url django.contrib import admin . import views # .views import ( # post_list, # post_create, # post_detail, # post_update, # post_delete, # ) # video: https://www.youtube.com/watch?annotation_id=annotation_3160187747&feature=iv&index=14&list=plesfxfp6dpzqfqfcur9cj4qnkqtvxusry&src_vid=nw3r2txlkwy&v=1kuyh8jvn6a # shows different solution here version 1.10 urlpatterns = [ #url(r'^$', views.post_home), # url(r'^$', views.post_list, name='list'), # url(r'^create/', views.post_create), #name='create' # url(r'^(?p<slug>[\w-]+)/', views.post_detail, name='detail'), # url(r'^(?p<slug>[\w-]+)/edit/', views.post_update, name='update'), # url(r'^(?p<slug>[\w-]+)/delete/', views.post_delete), url(r'^$', views.post_list, name='list'), url(r'^create/$', views.post_create, name='create'), url(r'^(?p<slug>[\w-]+)/$', views.post_detail, name='detail'), url(r'^(?p<slug>[\w-]+)/edit/$', views.post_update, name='update'), url(r'^(?p<slug>[\w-]+)/delete/$', views.post_delete), # url(r'^$', posts.views.post_home), # url(r'^create/', home_create), # url(r'^list/', home_list), # url(r'^detail/', home_detail), # url(r'^update/', home_update), # url(r'^delete/', home_delete), # url(r'^posts/$', "<appname>.views.<function_name>"), ]
post_list.py
{% extends "base.html" %} {% block head_title %} {{ instance.title }} | {{ block.super }} {% endblock head_title %} {% block content %} <div class="row justify-content-md-center"> <div class='col-md-8'> <h1>{{title}}</h1> </div> {% obj in object_list %} <div class='col-md-8 pt-3'> <div class="card" style="min-width: 20rem;"> {% if obj.image %} <img src="{{ obj.image.url }}" class="card-img-top"> {% endif %} <div class="card-body"> <h4 class="card-title"> <a href='{{ obj.get_absolute_url }}'>{{ obj.title }}</a> <small>{{ obj.timestamp|timesince }} ago</small> </h4> <p class="card-text">{{ obj.content|linebreaks|truncatechars:120 }}</p> <p class="card-text">{{ obj.updated }}</p> <p class="card-text">{{ obj.id }}</p> <!-- <a href='{% url "posts:detail" id=obj.id %}'>{{ obj.title }}</a><br/> --> <a href="{{ obj.get_absolute_url }}" class="btn btn-primary">view</a> </div> </div> </div> {% endfor %} <div class='col-md-8'> <div class="pagination"> <span class="step-links"> {% if object_list.has_previous %} <a href="?{{ page_request_var }}={{ object_list.previous_page_number }}">previous</a> {% endif %} <span class="current"> page {{ object_list.number }} of {{ object_list.paginator.num_pages }}. </span> {% if object_list.has_next %} <a href="?{{ page_request_var }}={{ object_list.next_page_number }}">next</a> {% endif %} </span> </div> </div> </div> {% endblock content %}
the error coming line
<!-- <a href='{% url "posts:detail" id=obj.id %}'>{{ obj.title }}</a><br/> -->
<!--
html comment, django still tries reverse url when renders template.
you can either remove line entirely, change django comment {# ... #}
, or change use slug instead of id.
wiki
Comments
Post a Comment