I am making a personal site. I have a blog page(site.com/blog), where I have a list of my blog posts. If I want to check a blog post simple enough I can click it and the code:
<h1>{{ obj.topic_title }}</h1>
will get me there. Also if I want to go to my contacts page (site.com/contacts). Easy enough I click nav contacs button and I go there
Contacts
but if I enter a blog post (site.com/blog/1), I am using the same template and if I want to go to my contacts page I have to yet again click the
Contacts
link, but that will port me to a 404 page site.com/blog/contacts . How do I deal with this problem without harcoding every single page
Use the built-in Django url template tag, which takes the view name and returns an absolute path to it.
Returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters.
You can give it a view name and any of its view parameters as well. This is much better than how you link to the blog page:
{% url 'blog-view-name' obj.id %}
This ensures that if you ever change the structure of your views, it will still not break your links.
Related
I want to save the internal URL (you can see in the picture below, the last input), and then access it from the code, so I will be able to change it from the admin panel, but it shows error like in image 2
you save django template of your url and not an url itself:
{% url 'main-page' %} # this is template
/main-page/ # this is relative url
in your case you can simply save the relative part of url. or you should before use 'url' in render made render_to_string.
more here: https://docs.djangoproject.com/en/4.1/topics/templates/#django.template.loader.render_to_string
I'm attempting to set a page as the root home page of my wagtail based site.
I know you can set the homepage using the sites setting. But, my use case is different.
I'm using a "redirect" page which redirects to an index page as the homepage. The index is restricted to only allow ertain page types (since it's an index....). But, this causes issues with other pages not being in the tree if it was set at the root.
Hence, the redirect. But, upon redirecting the serve view of the redirect page to use this page it picks up the slug.
I still want it to appear at the root url. Meaning, /. How can I achieve this?
High level description:
from django.shortcuts import redirect
class IndexPage(Page):
# children restricted to IndexSubpage
class IndexSubpage(Page):
# parent restricted to IndexPage
class RedirectPage(Page):
def serve(request):
# Link is a link to a page through a ForeignKey....
return redirect(self.link, permanent=True)
So, this will pick up the slug of IndexPage instead of being at /
I am working on a Wagtail project consisting of a few semi-static pages (homepage, about, etc.) and a blog. In the homepage, I wanted to list the latest blog entries, which I could do adding the following code to the HomePage model:
def blog_posts(self):
# Get list of live blog pages that are descendants of this page
posts = BlogPost.objects.live().order_by('-date_published')[:4]
return posts
def get_context(self, request):
context = super(HomePage, self).get_context(request)
context['posts'] = self.blog_posts()
return context
However, I would also like to add the last 3 entries in the footer, which is a common element of all the pages in the site. I'm not sure of what is the best way to do this — surely I could add similar code to all the models, but maybe there's a way to extend the Page class as a whole or somehow add "global" context? What is the best approach to do this?
This sounds like a good case for a custom template tag.
A good place for this would be in blog/templatetags/blog_tags.py:
import datetime
from django import template
from blog.models import BlogPost
register = template.Library()
#register.inclusion_tag('blog/includes/blog_posts.html', takes_context=True)
def latest_blog_posts(context):
""" Get list of live blog pages that are descendants of this page """
page = context['page']
posts = BlogPost.objects.descendant_of(page).live().public().order_by('-date_published')[:4]
return {'posts': posts}
You will need to add a partial template for this, at blog/templates/blog/includes/blog_posts.html. And then in each page template that must include this, include at the top:
{% load blog_tags %}
and in the desired location:
{% latest_blog_posts %}
I note that your code comment indicates you want descendants of the given page, but your code doesn't do that. I have included this in my example. Also, I have used an inclusion tag, so that you do not have to repeat the HTML for the blog listing on each page template that uses this custom template tag.
I'm a begginer grasping at straws with difficulty dealing with the django slug url system and these NoReverseMatch errors that make no sense to me even after reading the docs.
I have a django project. In one of the views, I pass a list of geoJSON features into a template, and show them on a map. I want to have each feature act as a clickable 'link' to a view that will show stuff about it. The following is part of the template that has those features that I want to click on:
//part of the template:
<script type="text/javascript">
...
function onEachFeature(feature, layer) {
layer.on('click', function (e) {
window.location.href = "{% url 'polls:areadetail' feature.properties.myslug%}";
});
}
(I have confirmed that feature.properties.myslug does in fact contain the slug I want).
The url pattern I want to go to:
urlpatterns = [...
url(r'^areadetail/(?P<areaslug>[-\w]+)/$', views.AreaDetail, name='areadetail'),]
And the view it relates to:
def AreaDetail(request, areaslug):
area = get_object_or_404(Area, nameslug=areaslug)
return render(request, 'polls/areadetail.html', {'area':area})
The issue I get is, by doing what I show and placing that url reference inside that template I show above, that I want to be able click on, that template won't even work at all, giving me a 'Error during template rendering' full page error info that starts with:
NoReverseMatch at /polls/areas/
Reverse for 'areadetail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/areadetail/(?P[-\w]+)/$']
Any help would be immensely appreciated
EDIT part1: As I've said in response to falsetru, I'm sure feature.properties.myslug has in fact got a slug expression in it.
EDIT2: Based on something I found in a django ticket, I've made a slight change in the url regex at urls.py, from (?P<areaslug>[-\w]+)/$ to (?P<areaslug>[-\w]+)?/$ and now the error is:
Page not found (404)
Request Method: GET Request URL: http://127.0.0.1:8000/polls/areadetail// Raised by: polls.views.AreaDetail
Is it possible that because the "{% url 'polls:areadetail' feature.properties.myslug%}" bit is inside javascript, that feature.properties.myslug is not being inserted there correctly? Like some sort of brackets are needed here?
According to the error message, feature.properties.myslug is empty or has no value.
Make sure the feature.properties.myslug is passed correctly from view.
Comment out {% url .. %} temporarily.
Print {{ feature }}, {{ feature.properties }}, {{ feature.properties.myslug }} to see if which part is missing.
Fix view accordingly.
Uncomment {% url .. %}.
After some more digging around I've found the answer to why doesn't this work in another question at:
How to pass javascript variable to django custom filter
The answer to it by Ludwik Trammer says:
Django templates are build on the server side, while JavaScript is executed on the client side.
That means that template code is always executed before JavaScript (as
it is executed by the server, before the page is sent to the client).
As a consequence it is absolutely impossible to mix JavaScript and
Django code the way you want to.
Which clearly applies here. I was focused on problems with the URL template, regex on the urls.py file etc. when the problem was that no matter what I did, because it's in a javascript section, run client-side, that URL template will always be incomplete no matter what I do, therefore being an impossible solution to what I want.
How do you point blogs to a menu item in Mezzanine? I am able to point my blogs to Home using urls.py but how about to page types like link and richtextpage?
Add a rich text page, call it Blog or however you want, then in meta data group, in the field URL add /blog/ or whichever is the url to the main blog app. Mezzanine will match the url with the page and will add the Page object to the rendering context, so you can use it in templates.