How to show flatpages with a single URL in Django? - django

I am trying to namespace Flatpages in Django. I included /pages/ in URLConf and added one URL /help/ in Django Admin Sites module. However, the page is loading with '/help/' and '/pages/help/' both URLs. I am trying to stop this behaviour and only load the page with /pages/help/. How is this possible?
urlpatterns = [
...
url(r'^pages/', include('django.contrib.flatpages.urls')),
]

You must have the fallback middleware installed in your MIDDLEWARE_CLASSES setting:
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
Remove it, and then it will only work on the prefix you have specified.

Related

How do I reference a path (URL) in Django? But this path is not in the base app, but in another app

In the base app, which I call it "mywebsite" (the one that contains the settings of the django project), has the urls.py file. But I do not want to reference this file, I want to reference a urls.py in another app, which I call it "account".
For the base file, I would reference as {% url'login' %}, for example. How do I reference to the other one?
Maybe {% account.url 'login' %}?
I tried {% account.url 'login' %} and {% account/url 'login' %}
Never mind, I just added to the base urls.py file: path('account', include("account.urls"))
I see that you already answered your question, but with no further details or explanations. I'll provide the context needed.
You have a project package. This folders contains files that affect all the application (the website, not just an app). Inside we have the following files: settings, wsgi, asgi and urls.
In the urls.py (also called URLconf module) file you control the routing of your application, that is, the mapping between URL path expressions to Python functions (your views).
The Django frameworks knows where to look up for this file because it's stablished in the settings.py file:
ROOT_URLCONF = 'django_project.urls'
This is the process (from the docs)
Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting...
Django loads that Python module and looks for the variable urlpatterns. This should be a sequence of django.urls.path() and/or django.urls.re_path() instances.
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.
Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view)...
But you also have apps, besides the project package. Each app can also have a URLconf module (app/urls.py).
To Django to know the routing to this apps, you have to include them in the global urls.py file (from the project package).
Finally, the answer:
How do you do it? With the include() function.
As you mentioned in your own answer, the code is:
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("account.urls")),
]
You're including the accounts paths into the global URLconf module.
So when the user types www.your-domain.com/ he will access the accounts app routes.
If instead you did like below, the user should type: www.your-domain.com/accounts/.
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include("account.urls")),
]

Django URL order

iam stuck again I hope I will get useful help this time too.
Iam trying to run the app but it gives me URL configuration error something like this:
Using the URLconf defined in pyshop.urls, Django tried these URL patterns, in this order:
admin/
products/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
I see a similar query in this platform but I couldn't find my answer
Iam using Django version 2.1
My code in pyshop.urls is:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
]
You don't route / anywhere, and
The empty path
implies that's exactly where you're visiting.
You'll either need to
add e.g. path('', SomeHomepageViewMaybe),
or remove the /products/ prefix to make the products app a "main" app (not that there's a concept like that in Django),
or just navigate to /products/ instead

Django Management command for Activating/Deactivating admin site

I would like to know if it's possible to 'Activate' and 'Deactivate' the admin site using a custom management command and what i mean by deactivating is commenting the line containing the path of admin.site.urls in the main urls.py file.
Or if there is a better way to do this.
I think the best way to do it - write custom middleware.
You can detect if current request is request to admin site (for example request.path.startswith('/admin')) and return 404 error if you disable you admin page.
When do you want to deactivate the admin urls? For example if you want to deactivate admin site urls for production, you can add a control to urls.py checkin if settings.DEBUG is False.
urlpatterns = [
...
]
if not settings.DEBUG:
urlpatterns += [
path('basic-admin/', basic_site.urls)
]

How can I fix my Wagtail URL namespace and explorer when adding wagtail to a current project?

My issue is my URL is coming up mysite.com/test-news-1/ instead of mysite.com/news/test-news-1/
I've started a new project using cookiecutter-django. I then wanted to add wagtail cms to the project and followed the docs to do so.
I get the wagtail admin page up fine, at /cms/ instead of /admin/ like it says to do on this page - http://docs.wagtail.io/en/v1.3.1/getting_started/integrating_into_django.html
I added a few apps just to practice and get used to wagtail, one directly copied from the wagtail blog example. This is where my issue starts.
The wagtail admin Explorer does not list my apps like it shows on the wagtail site https://wagtail.io/features/explorer/, instead it just says "Welcome to your new Wagtail site!" When I select Add Child Page it allows me to select the app pages I have set up and seems to go by my models just fine. But when I post something and click go to live site it comes up as mysite.com/blog1/ instead of mysite.com/blog/blog1/
I believe my problem that I dont understand the final part of the doc page that I linked above. It says,
Note that there’s one small difference when not using the Wagtail
project template: Wagtail creates an initial homepage of the basic
type Page, which does not include any content fields beyond the title.
You’ll probably want to replace this with your own HomePage class -
when you do so, ensure that you set up a site record (under Settings /
Sites in the Wagtail admin) to point to the new homepage.
I tried adding the homepage model from the doc page, but this didn't seem to help at all.
I'm very inexperienced, this is my urls.py file, if you need to see other files please let me know.
urls.py
from __future__ import unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailcore import urls as wagtail_urls
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'^about/$', TemplateView.as_view(template_name='pages/about.html'), name="about"),
# Django Admin, use {% url 'admin:index' %}
url(settings.ADMIN_URL, include(admin.site.urls)),
# User management
url(r'^users/', include("contestchampion.users.urls", namespace="users")),
url(r'^accounts/', include('allauth.urls')),
# Your stuff: custom urls includes go here
# Wagtail cms
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'', include(wagtail_urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
# This allows the error pages to be debugged during development, just visit
# these url in browser to see how these error pages look like.
urlpatterns += [
url(r'^400/$', default_views.bad_request, kwargs={'exception': Exception("Bad Request!")}),
url(r'^403/$', default_views.permission_denied, kwargs={'exception': Exception("Permission Denied")}),
url(r'^404/$', default_views.page_not_found, kwargs={'exception': Exception("Page not Found")}),
url(r'^500/$', default_views.server_error),
]
These two url confs are in conflict =
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'', include(wagtail_urls)),
One must change, otherwise Django will always resolve the base url of `yourdomain.com/' to the first entry. One easy way to fix this is to update the second-
url(r'^content/', include(wagtail_urls)),
Now your Wagtail root page will be accessible at yourdomain.com/content.
As for mysite.com/blog/blog1/ not coming up, that Url would assume that, from your Wagtail root page, there's a page w/ slug 'blog', and then a child page of that with slug blog1. The tree structure of your Wagtail site determines the URLs by default. If you want to override that you'll have to use the RoutablePageMixin as described here.

Can't get django registration to work

i have installed django-registration on django 1.3, I can't get it to work.
When i hit submit on the accounts/register/ page, it redirects to accounts/registration which isn't in the default URL's.
I can't seem to pass my post data to the register function. Help?
Does your urls.py have:
import registration
...
urlpatterns += patterns(
url(r'^accounts/', include('registration.backends.default.urls')),
)
Do you have all the appropriate templates installed in a template directory?
https://github.com/yourcelf/django-registration-defaults