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.
Related
I'm trying to deploy gatsby based frontend with django based backend under a single domain. It will rely on Apache and mod_wsgi. In a perfect world it should work as following:
https://my-domain.com/ - gatsby frontend
https://my-domain.com/admin - django
https://my-domain.com/api - django
I can see two possibilities:
Django is aware of frontend. Serve everything via Django, setup / as a STATIC_URL.
Django is not aware of frontend. Serve /api and /admin via django. / is handled by a webserver.
I feel more comfortable with the second approach, however I do not know how to configure VirtualHost for such a scenario. The firstch approach looks like an ugly hack.
How should I proceed with that?
After compiling your gatsby project, it should be served by django as a static page.
First: The gatsby dist should be in your static_private path.
Second: In your django project, you will define a URL for / that will call an index view let's say.
Finally: in your view you should render index.html of your gatsby dist.
urls.py:
from django.contrib import admin
from django.urls import path, re_path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('apis/', include('apps.urls')),
path('/', views.index),
]
views.py:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
Note that if you are handling routing in your frontend your url pattern for the index view should be like this : re_path('^.*$', views.index)
If you are hosting your django app on heroku you will need the whitenoise middleware and set it up in your settings.py :
MIDDLEWARE = [
...
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
A doc is available here: https://devcenter.heroku.com/articles/django-assets#whitenoise
Can't seem to find anything on this so far, I'm guessing I have something misconfigured somewhere.
I have a pre-existing Django app and am trying to use Wagtail to add a blog in. I have installed as per the instructions and I can access the default landing page so it seems to be installed however I'm stuck with the below error when attempting to access the admin for it and am not sure how to proceed. Guessing its something to do with not defining namespaces somewhere, the Wagtail docs say that its compatible with Django v1.11 but their integration documentation is for Django v2 specifically and is using re_path etc.
'wagtailadmin_api_v1' is not a registered namespace
My main project urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls', namespace='blog')),
url(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')),
url(r'^autocomplete/', include(ac_urls, namespace='ac')),
url(r'^', include('website.urls', namespace='website')),
]
My blog app's urls.py (which is fresh from a "python manage.py startapp blog")
from django.conf.urls import url, include
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.core import urls as wagtail_urls
app_name = 'blog'
urlpatterns = [
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'', include(wagtail_urls)),
]
When I attempt to access localhost:8000/blog/ I get the default wagtail landing page, if I go to localhost:8000/blog/cms/ I get the above error though. If I go to localhost:8000/blog/documents/ I get a 404 as well.
Not sure where I'm going wrong, have tried using a version of wagtail that still used the Django v1.11 way of routing in urls.py but I got the same error!
Over the weekend I attended a hackathon, at which I created a Django app that ran on 127.0.0.1:8000/myapp/list on my local machine with
python manage.py runserver
Today, I tried to launch another Django app that I had made in the past for bug fixes and things, but when I ran manage.py runserver it keeps redirecting me to 127.0.0.1:8000/myapp/list, even though it's a totally separate app.
Anyone know how to resolve this issue?
urls.py for Django app that I'm trying to run:
from django.conf.urls import url
from TweeTest import views
urlpatterns = [
url(r'^$', views.view_home, name='home'),
url(r'^about/$', views.AboutPageView.as_view()),
url(r'^contact/$', views.ContactPageView.as_view()),
url(r'^result/$', views.view_tweet, name = 'result'),
]
urls.py for Django app I made over the weekend:
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myproject.myapp.urls')),
url(r'^$', RedirectView.as_view(url='/myapp/list/', permanent=True)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Is it something to do with the RedirectView line in the second urls.py?
I'm not sure why a totally different Django app is trying to connect to the same URL as the other one... seems strange.
If someone knows the answer to this, that would be greatly appreciated!
Thanks!
UPDATE:
When I try to connect to 127.0.0.1:8000 on incognito mode, it works fine. Could this mean it's a browser issue?
Davis
permanent=True sends a HTTP 301 Permanent redirect to the browser. Browsers tend to cache 301 responses very aggressively, meaning your web browser currently remembers that http://127.0.0.1:8000/ is a permanent redirect to http://127.0.0.1:8000/myapp/list/. Since your browser doesn't know that different Django apps might be running on 127.0.0.1:8000 at different times, it always follows the cached redirect.
You shouldn't use permanent=True unless you know what you are doing. While it can boost the performance on a high-traffic site, it is rarely necessary for low- to medium-traffic sites, and the permanent caching can be a real pain for developers.
Either clear your browser cache, run your Django test server on a different port, or use a different browser.
Have you tried using a different browser or clearing your browser history? This sounds like a browser issue to me.
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.
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.