I'm trying to reverse(just the way reverse_lazy works) the url_name stored in the SubMenu.link
Here is my code
My Model
class SubMenu(models.Model):
menu = models.ForeignKey(MainMenu, on_delete=models.CASCADE, related_name="submenus")
title = models.CharField(max_length=50)
link = models.CharField(max_length=50, null=True, default="null")
def __str__(self):
return self.title
My root urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('bodys.urls')),
path('blog', include('blogs.urls')),
path('contact', include('contacts.urls')),
path('services/', include('services.urls')),
]
I created a sitemap as follows, but nothing is displayed inside the sitemap URL.
How can I fix the problem?
Thank you
setting.py
INSTALLED_APPS = [
'django.contrib.sitemaps',
]
sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.shortcuts import reverse
from riposts.models import Posts
class RiSitemap(Sitemap):
priority = 0.5
changefreq = 'daily'
def get_queryset(self):
posts = self.kwargs.get('posts')
return Posts.objects.filter(status="p")
def lastmod(self, obj):
return obj.updated
def location(self, item):
return reverse(item)
urls.py
from django.contrib.sitemaps.views import sitemap
from .views import home_page
from riposts.sitemaps import RiSitemap
sitemaps = {
'posts':RiSitemap,
}
urlpatterns = [
path('', home_page, name="home"),
path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="sitemap"),
]
sitemap image
I am writing a blog application but I got stuck.
I'm facing the following error:
':'.join(parents + (url.namespace,)) for url in url_patterns RecursionError: maximum recursion depth exceeded
I got two urls.py files.
mysite/urls.py:
from django.conf.urls import url
from quickblog import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
mysite/myapp/urls.py:
from django.conf.urls import include
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('quickblog.urls')),
]
mysite/myapp/models.py:
from django.db import models
from django.utils import timezone
class Post(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
/mysite/myapp/admin.py:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
I guess I'm doing something wrong here. Any hint would be nice.
It seems you have swapped urls.py data
mysite/urls.py should be:
from django.conf.urls import include
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('quickblog.urls')),
]
mysite/myapp/urls.py should be:
from django.conf.urls import url
from quickblog import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
sometimes when i want to open the about page, the contact, create or blog
page this error comes: Raised by: blog.views.blog_postDetailView
i donnt know why, but I think it has to do sth with my urlpattterns
or the order of them . here my code.
appreciate your help and improvements
thanks a lot
mainurl
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from blog.views import AboutPageView, ContactPageView, blog_postCreateView
urlpatterns = [
url(r'^about/', AboutPageView.as_view(), name='about'),
url(r'^contact/', ContactPageView.as_view(), name='contact'),
url(r'^create/', blog_postCreateView.as_view(), name='blog_post_create'),
url(r'', include('blog.urls')),
url(r'^blog/', include('blog.urls')),
#admin and login
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('registration.backends.default.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
blog url
from django.conf.urls import url
from .views import blog_postListView, blog_postDetailView, blog_postCreateView
urlpatterns = [
url(r'^$', blog_postListView.as_view(), name='blog_post_list'),
url(r'^(?P<slug>[-\w]+)$', blog_postDetailView.as_view(), name='blog_post_detail'),
]
base html, the nav part
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
Home
</li>
<li>
About
</li>
<li>
Contact
</li>
<li>
Blog
</li>
<li>
{% if request.user.is_authenticated %}
<li>Logout</li>
{% else %}
<li>Login</li>
<li>Register</li>
{% endif %}
model
rom __future__ import unicode_literals
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import pre_save, post_save
from django.utils.text import slugify
# Create your models here.
def upload_location(instance, filename):
#filebase, extension = filename.split(".")
#return "%s/%s.%s" %(instance.id, instance.id, extension)
return "%s/%s" %(instance.id, filename)
class blog_post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=120)
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
image = models.ImageField(upload_to=upload_location, null=True, blank=True, width_field="width_field", height_field="height_field")
#height_field = models.PositiveIntegerField()
#width_field = models.PositiveIntegerField()
slug = models.SlugField(unique=True)
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("blog_post_detail", kwargs={"slug": self.slug})
def create_slug(instance, new_slug=None):
slug = slugify(instance.title)
if new_slug is not None:
slug = new_slug
qs = blog_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=blog_post)
views
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.views.generic import View
from django.views.generic.base import TemplateView, TemplateResponseMixin, ContextMixin
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.shortcuts import render
from django.utils.decorators import method_decorator
from .models import blog_post
from .forms import blog_postForm
# Create your views here.
class LoginRequiredMixin(object):
#classmethod
def as_view(cls, **kwargs):
view = super(LoginRequiredMixin, cls).as_view(**kwargs)
return login_required(view)
##method_decorator(login_required)
#def dispatch(self, request, *args, **kwargs):
# return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
class blog_postCreateView(LoginRequiredMixin, CreateView):
#model = blog_post
form_class = blog_postForm
template_name = "form.html"
#fields = ["title", "content"]
def get_success_url(self):
return reverse("blog_post_list")
# #method_decorator(login_required)
# def dispatch(self, request, *args, **kwargs):
# return super(MyView, self).dispatch(request, *args, **kwargs)
class blog_postListView(ListView):
model = blog_post
def get_queryset(self, *args, **kwargs):
qs = super(blog_postListView, self).get_queryset(*args, **kwargs).order_by("-timestamp")
return qs
class blog_postDetailView(DetailView):
model = blog_post
class AboutPageView(TemplateView):
template_name = "about.html"
class ContactPageView(TemplateView):
template_name = "contact.html"
updated urls
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from blog.views import AboutPageView, ContactPageView, blog_postCreateView
urlpatterns = [
url(r'^about/', AboutPageView.as_view(), name='about'),
url(r'^contact/', ContactPageView.as_view(), name='contact'),
url(r'^create/', blog_postCreateView.as_view(), name='blog_post_create'),
url(r'^categories/', include('blog.urls_categories')),
#url(r'', include('blog.urls')),
url(r'^blog/', include('blog.urls')),
#admin and login
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'', include('blog.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Put the below line in end of url_patterns list like
urlpatterns = [
#all other urls,
url(r'', include('blog.urls')),
]
OR
url(r'^blogdetail/', include('blog.urls')),
views.py
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
from django.shortcuts import get_list_or_404
from django.template.context import RequestContext
from articles.models import Article
def index(request):
articles = get_list_or_404(Article)
return render_to_response(
'articles/index.html',
{"articles": articles},
context_instance=RequestContext(request),
mimetype="application/xhtml+xml")
def article(request, article_id):
article = get_object_or_404(Article, pk=article_id)
return render_to_response(
'articles/article.html',
{"article": article},
context_instance=RequestContext(request),
mimetype="application/xhtml+xml")
models
from django.db import models
from django.contrib.auth.models import User
import datetime
class Article(models.Model):
"""
Article model
"""
title = models.CharField(blank=True, max_length=200)
slug = models.SlugField()
body = models.TextField(blank=True)
created = models.DateTimeField(blank=True, default=datetime.datetime.now)
author = models.ForeignKey(User)
def __unicode__(self):
return "%s" % (self.title)
#property
def handle(self):
return self.slug
urls
from django.conf.urls.defaults import *
urlpatterns = patterns('',
url(
r'^$',
'articles.views.index',
name="articles_index"
),
url(
r'^article/(?P<article_id>\d*)$',
'articles.views.article',
name="article_view",
),
)
root urls
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from settings import PROJECT_ROOT
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
urlpatterns += patterns('',
(r'^articles/', include('articles.urls')),
)
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': PROJECT_ROOT + "/media"}),
)
Would you need to see more?
I had a theory that the word 'Article' might have conflicted with something, tho I tried renaming that to no avail.
This is supposed to be just a little 'play app' that I'm learning on. But now I'm quite stuck.
Running: python manage.py runserver_plus
http://127.0.0.1:8000/admin (views work ok)
http://127.0.0.1:8000/articles (crashes python everytime)
Quite a hair pulling exercise... help very much appreciated
edit
the error report:
http://dpaste.org/8Fzx/
Thank you!
Answer: self referencing template!