python - Django template failling despite my effort to learn it -
categories.html # want called categoryview.as_view()
never works. howevwer index.html works have hardcoded link follows within index.html. although index.html extends base.html outputing url. edit: avoid confusion available in index.html template guess should work shows following error index page not found (404)
<li><a href="{% url 'all_categories' %}">all</a></li>
from root directory when type http://127.0.0.1:8000 works want http://127.0.0.1:8000/cartegories/
(index.html
or categories.html
) assume index.html or categories.html not visible when typed, fine.
i can’t believe taking me 12hours possible intuitive tweaking still can’t work. please advise doing wrong. in essense outputing hellow world in categories.html later iterate through , list categories in blog… if hello world
can’t work nothing will..
#posts urls.py django.conf.urls import include, url django.contrib import admin .views import indexview, categoryview urlpatterns = [ url(r'^$', indexview.as_view(), name='index'), url(r'^all_categories$', categoryview.as_view(), name='all_categories'), # stuff have tried ] #admin urls.py django.conf.urls import include, url django.contrib import admin urlpatterns = [ url(r'^/', include('posts.urls', namespace="posts")), url(r'^admin/', include(admin.site.urls)), ] #views.py django.shortcuts import render .models import post, comment, category, reply django.views import generic class indexview(generic.listview): template_name = 'posts/index.html' context_object_name = 'posts' model = post def get_context_data(self, **kwargs): context = super(indexview, self).get_context_data(**kwargs) context['categories'] = category.objects.all() return context class categoryview(generic.listview): template_name = 'posts/categories.html' context_object_name = 'categories' model = category #modesl.py class category(models.model): category_name = models.charfield(max_length=200) class meta: verbose_name_plural = "categories" def __str__(self): return self.category_name def get_absolute_url(self): return reverse('all_categories') class post(models.model): title = models.charfield(max_length=200) … category = models.foreignkey(category) def save(self, *args, **kargs): if not self.id: self.slug = slugify(self.title) super(post, self).save(*args, **kargs) def __str__(self): return self.title
changing order of urls in files solved problem. generally, order of main urls.py
important readability /
slash did trick
#admin urls.py django.conf.urls import include, url django.contrib import admin urlpatterns = [ url(r'^/', include('posts.urls', namespace="posts")), url(r'^admin/', include(admin.site.urls)), ]
also in post/url.py file added /
@ end of regex
urlpatterns = [ url(r'^$', indexview.as_view(), name='index'), url(r'^categories/$', categoryview.as_view(), name='all_categories'), ]
finally in template html
read follows:
<li><a href="{% url 'posts:all_categories' %}">all</a></li>
Comments
Post a Comment