I have a Django CMS Project, which needs to create a Non CMS App "Achievemnets". The Customer wants full control over the page design, that means the page should be a CMS Page. However I have created specific views to show all the achievemtns in a page and clicking on the more link, it will show in detail. I need to port it to Django CMS I have tried as per the CMS App Hook method in the Django CMS Documentation. But none of them works.
Please tell me a tutorial which is good for learning CMS App Hooking
When you "hook" an application's URLs to a Django-CMS page, your app's URLs and view functions take over from there.
Let's say your Django-CMS page URL is: /achievements/
On this page, you want to display a list of achievements, which is going to come from your application.
#your_app.urls
from django.conf.urls.defaults import url, patterns
urlpatterns = patterns('your_app.views',
(r'^$', 'index'),
)
#your_app.views
from django.shortcuts import render
from your_app.models import Achievement
def index(request):
achievements = Achievement.objects.all()
return render(request, 'achievements/index.html',
{'achievements' : achievements})
The Django-CMS app hook you write tells Django-CMS which URLs to follow after in addition to the page you hook your app to. So, not only is Django-CMS going to pull content for the page by the slug, it's also going to hand off the matching URL pattern to your app.
I hope that makes sense.
Related
Homepage doesn't load properly
I have successfully build and deployed my code in Heroku.
It works fine locally but react doesn't render the index.html
The backend API works properly here
but the homepage here doesn't load - using ReactJS.
My all GitHub codes are here inside the develop branch.
Followed this post steps to host the same.
The homepage does not have a proper URL or path defined.
For example, you can go here:
https://mangya.herokuapp.com/administrator/
Or here
https://mangya.herokuapp.com/api
...As they are valid URLs.
Your blank 'homepage' path is not listed so there is no view being hit thus you get the error.
To fix this you need to change your urls.py in MyBlog to look like this:
urlpatterns = [
path('', [path.to.your.view]),
path('administrator/', admin.site.urls),
path('api/', include(router.urls))
]
In Django for anything to be rendered when you hit a URL, you need to have a view defined that renders the template, this is what my example for your urls.py shows.
So for example, if you are trying to run a Vue, React, or any other frontend framework that would rely on an API and Ajax calls to populate a page, you must allow that base page to be rendered by Django as it is the server running your application.
I'm trying to boot up a Django CMS app. All the app hooks are properly set up and registered. For an example take the NewsHook:
class NewsHook(CMSApp):
""" A class to hook the News into the django cms
"""
name = ("News")
urls = ["apps.news.urls"]
apphook_pool.register(NewsHook)
The urls.py of this hook includes the following:
urlpatterns = [
# /feed/
url(r'^feed/$', ArticlesFeed(), name='news_feed'),
]
And the urls.py of the project (under the settings folder) includes the following relevant lines:
admin.autodiscover()
urlpatterns = patterns(
'',
...
# / -> Django CMS
url(r'^', include('cms.urls')),
)
All this looks normal, right? But when I visit the home page, I get NoReverseMatch error:
Not sure what I am doing wrong... Is there a side of this that I'm not seeing? Btw, this app runs well on production, so it doesn't have any bugs as far as I can see.
My Specs
Django version: 1.8.13
Django CMS version: 3.3.0
Python version: 2.7.
I discovered that these Django CMS apps need to be attached to a page in order to add the urls to the project. I loaded the pages table from the production and the urls started functioning.
Making the urls dependent on the presence of a page is really counterintuitive to me, coming from Rails.
I am working Django 1.8 and having trouble using my custom login page. Whenever I go to my login url, the Django admin username and password form comes up instead of my custom login.html.
I have worked in the Django 1.7 and this works fine but I do not know why I am having the error in 1.8.
login.html is my custom login template.
My urls:
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}, name='auth_login'),
I have checked the forums and the Google but still cannot find a similar error.
EDIT:
Answer thanks to Kishor.
In Django 1.8 I simply renamed the admin pages that I was using, namely, base.html, base_site.html and login.html [I store these files inside a folder called 'admin' which is inside my app's 'templates' folder]
Instead of using the normal way like in previous versions of Django i just called the three pages above myapp_base.html, myapp_base_site.html and myapp_login.html [Do not forget to also change the tag at the top inside these pages where is says 'extends']
From there my app was able to pick up that I wanted to override the standard Django login page and customize it to my specification.
Use this 1.8 way/generic view way
url(r'^accounts/login/$', auth_views.login, {'template_name': 'myapp/login.html'}),
and be sure that you have included
url('^', include('django.contrib.auth.urls'))
I want to be able to display a 404 page when any other request occurs. However, when I run the server and perform a POST request with the POSTMAN extension on chrome, I get a 403 display by Django and not the template I created.
proj/urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include("index.urls")),
url(r'auth/', include("userapp.urls"))
)
index/urls.py
urlpatterns = patterns('',
url(r'^$', load_index),
url(r'^admin/', include(admin.site.urls)),
)
index/views.py
def load_index(request):
if request.method == "GET":
form = RegistrationForm(auto_id=False)
return render(request, "index/index.html", {"form": form})
else:
# output 404 error
return render(request, "error/404.html", status=404)
When you create a Django web application, POST/PUT/DELETE requires the csrf token. It's kind of a security policy which Django follows.
Now, why is this required? Because, django web applications by conventions are app's that would have been run with keeping in mind the context of display and usage.
What you are trying to use is the Client like POSTMAN which is used mainly to test the REST services. (It does not mean you cannot fire requests for an web application that is hosted to work as an Html template).
Now, if you want to provide a POSTable API from your Django web application, consider the #csrf_exempt. The best name given for a topic: csrf-protection-should-be-disabled-for-just-a-few-views.
If you want your application to have, it's usage as a RESTFul service, make sure you use the right tool, might be Django Rest Framework. (Note: Using #csrf_extempt is also a way for you, if you are providing an API to your site, which would provide some views as an API for POST/PUT).
I have to include multiple changes to djangos admin panel, so I decided to fork the django admin app into my own django project.
As I was working with this admin app I recognized, that the site registration and template handling differs from the apps, that are normally created in django.
For instance, I want to keep the old admin index.html template and view, for backup and safety reasons but the landing page should be replaced by a custom page.
For that of course I need to change admin/templates/index.html and /admin/sites.py respectively.
I copied the old index function in admin/sites.py to old_index.py and created a old_index.html in the template folder.
But if I try to reference to old_index.html in my new index.html with
old index
I got an NoReverseMatch-Exception thrown. Unfortunately I did not found more information about how the django admin app itself register new views and sites, so an example or description would be helpful.
Creating separate views for the admin app in the distinct other apps in my project is no real option, due the high amount of changes, that need to be done.
The main urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'django_project.views.home', name='home'),
url(r'^polls/', include('other_app.urls', namespace="other_app")),
url(r'^admin/', include(admin.site.urls)),
)
The admin app itself does not provide a urls.py file and the views.py is exactely the same as in django.contrib.admin I just copied the function index to a new function called old_index, referencing to a template old_index.html.
Maybe the point did not get so clear, as I expected. I copied the whole admin app in my project and want to add a custom defined site to it, regardless where. But I failed to understand how sites and views are registered in the admin app itself, because the way is different from the custom apps you create normally in django.
So, is it possible (and how) to add a custom site in the django.contrib.admin app?
I think you need to create your own AdminSite for custom purposes and keep default as it is. More about this you can find here: https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#adminsite-objects and here https://docs.djangoproject.com/en/dev/ref/contrib/admin/#multiple-admin-sites-in-the-same-urlconf
Update:
You need to edit get_urls method of AdminSite class - add:
url(r'^$', wrap(self.old_index), name='old_index')
to urlpatterns variable. And rename old index method to old_index.