How to get site settings in a django project converted to wagtail - django

I have a django site onto which I have added wagtail following these instructions.
I have set up Social Media setting using these instructions
I have sucessfully added details on the admin page ad I can return and edit these details
However I cannot access them in a template
If I display
{{settings.site_settings.SiteSettings.facebook}}
I get '' (tested using {% if settings.site_settings.SiteSettings.facebook == '' ...)
However
{{settings.site_settings.SiteSettings}}
returns None
and
{{settings.site_settings}}
returns SettingsModuleProxy(site_settings)
What am I doing wrong?

The problem was simply that I had misspelt the SiteSettings class name in models.py (very red-face)

Related

How to serve Multilingual URLs in Django CMS?

I'm trying to internationalize my site with Django CMS 3.7.
The language code and content are all setup fine, but I couldn't setup the URL with multilingual.
When I tried to set URL with another language (e.g. Chinese) I got the error message: "Slug must not be empty."
Only English works. See screenshot attached.
Any ideas to fix this?
You just have to put a slug for each language on each page, the slug is what will be shown in the url after the language code.
Example:
Page: Home
ENG:
slug = home
ESP:
slug = inicio
.
.
.

LOGIN_REDIRECT_URL in django

I am very new to Django and I'm nearing the end of the django girls tutorial. I have added "#login_required" above my post_detail in views (view for clicking on a specific post) and added a login.html template. So when I click on a post title I get redirected to my login page (so far, so good) and the url is: http://127.0.0.1:8000/accounts/login/?next=/post/11/ (trying this on my computer atm.)
Then I type in my admin name/password and automatically get redirected to http://127.0.0.1:8000/accounts/profile/ and of course get a "Page not found (404)" (since I have no url/view/template for that). I thought "Dang, I just wanted to be redirected to /post/11/"!
Looked around on stack overflow and found this question:
Signing in leads to "/accounts/profile/" in Django (sounds about right)
and got the answer
Change the value of LOGIN_REDIRECT_URL in your settings.py.
So I looked up LOGIN_REDIRECT_URL in the Django documentation:
Default: '/accounts/profile/'
The URL where requests are redirected after login when the contrib.auth.login view gets no next parameter.
This is used by the login_required() decorator, for example.
This setting also accepts named URL patterns which can be used to reduce configuration duplication since you don’t have to define the URL in two places (settings and URLconf).
Deprecated since version 1.8: The setting may also be a dotted Python path to a view function. Support for this will be removed in Django 1.10.
But doesn't my contrib.auth.login get a next parameter? (looking at my url that say "?next=/post/11/" at the end) Please help me out here, I'm lost for what the problem could be here :(
You can view the page at:
http://finbel.pythonanywhere.com/
And the source code at:
https://github.com/Finbel/my-first-blog
UPDATE (1):
So I now know that the LOGIN_REDIRECT_URL is the thing that's deciding where I end up next, which must mean that it ignores the next-parameter in the url. I googled further on the problem and found this question which was very similar to my problem, i.e.
Documentation states that I need to use the "next" parameter and context processors. I have the {{next}} in my template, but I'm confused on how to actually pass the "/gallery/(username)". Any help would be greatly appreciated.
(I don't even have the {{next}} in my template, where/how should I add it?)
The preferred answer to that question seemed to be:
Django's login view django.contrib.auth.views.login accepts a dictionary named extra_context. The values in the dictionary are directly passed to the template. So you can use that to set the next parameter. Once that is done, you can set a hidden field with name next and value {{ next }} so that it gets rendered in the template.
But I'm not sure how to interpret this. While writing this edit I got an answer on this post (by kacperd) and will read it through now)
The problem is that contrib.auth.login doesn't get the next parameter.
When you try to get the login_required view without credentials your request is redirect to login view, and the template you created is rendered. The next parameter is present in this view, but when you perform the login action which is submitting the form, you are not including next in your request so contrib.auth.login doesn't get it and redirects to default page.
The solution to your problem is to include the next param and pass it forward. You can do this by modifying your login template. Simply add ?next={{ request.GET.next }} to form action attribute.
<form method="post" action="{% url 'django.contrib.auth.views.login' %}?next={{ request.GET.next }}">

django-tables2 and LinkColumn. Correct path to Django-admin site page

I am developing cmdb application and trying to create a link to admin page of the device ( /admin/cmdb/device/device_id/) in django-tables2 LinkColumn with the following syntax:
id = tables.LinkColumn('admin:cmdb:device', args=[A('pk')])
This fails with error
NoReverseMatch at /cmdb/emp/171/
'cmdb' is not a registered namespace inside 'admin'
(/cmdb/emp/171/ - is the page on which the table is rendered)
How can I write the correct path in LinkColumn argument to Django admin page?
The goal could be achieved by using TemplateColumn:
id2 = tables.TemplateColumn('{{record.id}}')
but possibly someone could advise how to use LinkColumn?
Your question is not about the LinkColumn but about finding out the url names of the django admin pages.
In any case, you can find your answer here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls
So if the name of your application is cmdb and the name of your model is device, the url name of the device edit page would be admin:cmdb_device_change which could be used in the LinkColumn (also it can be used in the TemplateColumn using {% url "admin:cmdb_device_chang" record.id %}).

Check whether a page exists in Django CMS

I am creating a page in the admin area of Django CMS and I have the redirect field under Advanced Settings. How can I check that the URL entered in that field is a valid URL of an existing Django CMS page?
What should I test? I thought about issuing a request to that URL and if it throws a 404, then invalidate the field, but this sounds a bit too far fetched. What other options do I have?
You can check if your actual page is in a pool of pages
if your page is on the draft mode:
from cms.models import Page
your_page.get_path() in [p.get_path() for p in Page.objects.public().published()]
with a reverse_id:
your_page.get_path() in [p.get_path() for p in Page.objects.all() if p.reverse_id != your_page.reverse_id]
I've used get_page_queryset_from_path from cms.utils.page_resolver and checked that the path entered on the redirect field actually returns a valid Page using the above function.

Url mismatch, django template link

I've got a prob with a link in the sidebar of my django site, in the template it's like that:
<li>Profile</li>
while in the urls.py:
url(r'^(?P<user_id>\d+)/profile/$', 'auth.views.show_profile', name='profile')
When i access it from the main page with url: e.g /1001/profile/ it loads fine but when I try to access it from another subpage with url: e.g /1001/forms/profile/ i get the error: The current URL, /1001/forms/profile/, didn't match any of these. How can i fix this?
It is because "profile" is a relative URL, and a relative URL is appended to the current URL - the resulting address is not valid across the whole site. Seems like you should use an absolute URL in your case.
At the template you can try something like:
Profile
UPDATE
To get request available in templates you have to add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS. I'm not sure if it is added by default.
You must have to add your second subpage url in urls like you did for /1001/profile/
url(r'^(?P<user_id>\d+)/form/profile/$', 'auth.views.show_profile', name='profile_form')
and also correct your code as #Paulo mentioned or you can also do it through reverse url.
Profile