Django Mezzanine CMS base urls.py location? - python

I'm a Django newbie and looking at Mezzanine CMS and looked at all URLs, can somebody point where http://127.0.0.1:8000/about/ is defined, what urls.py, I'm guessing it is
urlpatterns = [
url("^(?P<slug>.*)%s$" % ("/" if settings.APPEND_SLASH else ""),
views.page, name="page"),
]
is that correct?

That is correct. Mezzanine page urls are not defined explicitly in a urls.py, but are stored in the database in the Page model's slug field. You can navigate to the "about" page and the admin and modify its slug field there.
Note that in practice, page views are always intercepted and returned by mezzanine.pages.middleware.PageMiddleware, which could be relevant for debugging purposes.

Related

How to hide the next parameter appearing to the url in Django?

When I am starting my Django project from the login page the url is showing like this:
http://127.0.0.1:8000/login/?next=/
But what i want only this:
http://127.0.0.1:8000/login
What is the way to hide this next parameter appearing to the url?
I have set login url and login redirect urls to the settings.py:
settings.py
LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'
urls.py
urlpatterns = [
url(r'^$',views.index,name='home'),
url(r'login/$',views.userLogin,name='login'),
]
This behavior is located in the AccessMixin in django.contrib.auth.
If you dislike it - inherit from LoginRequiredMixin and overwrite redirect_field_name to return '' or None. You need to use this mixin afterwards for the views for which you want to enforce login.
You should have a good reason for it. Usually as already mentioned in the comments above this behavior is useful.

Django 1.6 - templates for password change/reset

I want to use django's password change/reset views so for example for the view
django.contrib.auth.views.password_change
I need create a template named
registration/password_change_form.html
the problem is that even though I have this template implemented in my project, django still shows the password-change page of the admin website, the only way I can make django use my template is by renaming it to something different - like registration/password_change_form_1.html and then pass the name
url(r'^password/change/$',
auth_views.password_change,
{'template_name': 'registration/password_change_form_1.html',
'password_change_form': MyPasswordChangeForm},
name='password_change'),
Am I missing something here? why won't django use my template when I use the default name?
I think because your app is under django.contribute.admin in the INSTALLED_APP.
Django automatically generates the admin template with the default name, so, if you use the admin, you must specify a different template name.
It simply fails to find your template, since it is overiden by the generated one.
Add in settings
INSTALLED_APPS = (
...
'registration',
)
After
TEMPLATE_DIRS = (
...
"/home/user/templates",
)
Add in directory templates "registration"
base.html that will contain the template and other templates
run in django==1.5

New django template

I am trying to edit some things in django and am running into a few problems as I am quite new to it.
I am working off an existing django configuration with templates that currently work, but I don't really understand how to create new templates. If I create a new .html file in my templates folder in "myprojects" and then declare the new template under cms_templates = (...) in settings.py nothing happens. Am I missing a step to get the template to upload and appear in my cms?
I also had a problem with an existing template, when I try to go to the contact page I am told that the template does not exist: http://paul.evansoderberg.com/en/contact/
The template is declared under "cms_templates", the file exists in the templates folder, and the page is created in django cms yet I get this error message.
Help would be greatly appreciated, thank you.
In settings I have :
CMS_TEMPLATES = (
('index.html', 'index'),
('contact.html', 'contact'),
('main.html', 'main'),
('about.html', 'about'),
('cv.html', 'cv'),
)
The template field in admin is set to "contact"

Django admin: removing app name from URL for single-app projects

I am writing a simple Django app - say, called base - which has a few models. I am using Django's built-in admin site to manipulate the app's data. The admin is accessible via the default ^admin/ URL pattern.
Aside from Django's default INSTALLED_APPS (minus django.contrib.sites), base is the only app installed in my project. Is there any way to remove base/ from the URL, such that I can access base's models by simply using a path such as /admin/model/ instead of /admin/base/model/?
I would ideally like django.contrib.auth's models to still be accessible via /admin/auth/.
If you don't want base in the URL, don't put it there. It's not there unless you have specifically asked it to be. Just create your URLs without that prefix.
Edit Apologies, I misread your question: I thought you were asking about your app's own views, not the sub-sections with admin.
This is tricky. One way of doing it would be to use the hooks for adding URLs to the base AdminSite as described in the docs. You will probably need to copy the code from the ModelAdmin.get_urls method and hard-code the model name, since there won't be a way of doing that automatically.
Customizing or overriding your default Django admin site is quite easy. Here's the Django documentation on this. The following is an example of overriding the default admin site.
Create an admin.py in your Django project directory (if it's not there yet). Subclass the AdminSite.
To remove the 'appname' from the admin URLs override the get_urls() function:
# myproject/admin.py
from django.contrib import admin
class MyAdminSite(admin.AdminSite):
def get_urls(self):
urlpatterns = super().get_urls()
for model, model_admin in self._registry.items():
urlpatterns += [
path('%s/' % (model._meta.model_name), include(model_admin.urls)),
]
return urlpatterns
Creae an apps.py in your project directory (if it's not there yet):
# myproject/admin.py
from django.contrib.admin.apps import AdminConfig
class MyAdminConfig(AdminConfig):
default_site = 'myproject.admin.MyAdminSite'
Register this in your settings.py:
INSTALLED_APPS = [
...
'myproject.apps.MyAdminConfig', # replaces 'django.contrib.admin'
...
]

"admin" Subdirectory Triggering (unintentially) Django Admin Pages

I am having trouble with some custom admin pages triggering the Django admin site instead of displaying my custom pages.
My urls.py follows:
urlpatterns = patterns('',
# ... trimmed ...
# Admin pages
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
# Lobby Visitor Log
url(r'^visitorLog', include('lobbyVisitorLog.urls')),
)
In my lobbyVisitorLog app I have the following directory structure, leading to "admin" pages
lobbyVisitorLog
- templates
- admin
And my lobbyVisitorLog/urls.py follows:
urlpatterns = patterns('visitorLog.views',
url(r'^/$', views.home, name='homeView'),
url(r'^/search', views.search, name='searchView'),
url(r'^/submit', views.submit, name='submitView'),
url(r'^/admin/$', views.adminView, name='adminView'),
url(r'^/admin/import/$', views.adminImportView, name='adminImportView'),
url(r'^/(?P<guest_type>\w+)$', views.logEntry, name='logEntryView'),
)
The views.py for the admin index page looks like this:
def adminView(request):
return render(request, 'admin/index.html', {}, context_instance=RequestContext(request))
When I go to "mysite/visitorLog/admin/" I get the Django admin site with the following message: “You don't have permission to edit anything.”
However, if I change my "admin" directory to "utils" (or whatever else, other then "admin") and update my views.py accordingly, everything appears as expected! This is okay, I can deal with my directory being called "utils" but it will annoy me... just enough.
What is happening that is causing the Django admin page to load instead of my custom pages?
By default django first checks each of the paths you have in TEMPLATE_DIRS for 'admin/index.html'. If it doesn't find it there, it starts searching in the templates directory for each app in the INSTALLED_APPS setting.
If 'django.contrib.admin' is listed first in INSTALLED_APPS, it will use the identically named 'admin/index.html' template from the django.contrib.admin app.
Moving 'django.contrib.admin' to the last position in INSTALLED_APPS should allow it to find 'admin/index.html' in your lobbyVisitorLog app first, but this will break the admin site by cause it to use 'admin/index.html' from your app, lobbyVisitorLog.
A good way to solve this is to always have a sub-directory named after your app within your app's templates directory. For example:
lobbyVisitorLog
- templates
- lobbyVisitorLog
- admin
- index.html
and then update your view's template path:
def adminView(request):
return render(request, 'lobbyVisitorLog/admin/index.html', {}, context_instance=RequestContext(request))
You can find more on how Django loads templates here

Categories