I'm starting using Wagtail + Django.
Generally, in Django you set the urls path in a urls.py file.
However, I cannot find the url.py that says to my app, when users visits main domain (local http://127.0.0.1:8000/) show home_page.html view.
I'm following the get_started tutorial.
And used this command to generate the basic apps:
wagtail start elim
This generated: a) elim, b) home, c) search apps.
Only elim app contains a urls.py, but it doesn't set a path for home:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from search import views as search_views
urlpatterns = [
url(r'^django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
]
This is my project structure:
So how does going to "/" open the home page???
The home dir is served by wagtail internal mechanism. Scroll to the end of the file elim/urls.py to find this :
urlpatterns = urlpatterns + [
# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
url(r"", include(wagtail_urls)),
# Alternatively, if you want Wagtail pages to be served from a subpath
# of your site, rather than the site root:
# url(r"^pages/", include(wagtail_urls)),
]
So, continue to read the tutorial, I'm sure you will soon or later discover the Page models and everything provided by wagtail.
Try adding a view function in your app's views.py file that renders home_page.html when the user goes to "/".
in views.py write this:
def home_page(response):
return render(response, "home/home_page.html")
then map this to ur urs.py file
url_patterns = [
path("", views.home_page)
]
then add this url to your url conf
Related
My Django site isn't working properly. It's hard to explain but when I run my Django web server and go to http://127.0.0.1:8000/hello/ I see "Hello, World!" as expected. But when I go to http://127.0.0.1:8000/dashboard/ I see the same thing, when I should be seeing "Hello". There is to much code to put on stack overflow and it won't make sense so I made a GitHub repo https://github.com/Unidentified539/stackoverflow.
So to simplify your life
urlpatterns = [
path("",views.pomo,name="pomo"),
path("agenda/",views.agenda,name="agenda"),
path("notes/",views.notes,name="notes"),
]
this is your urls.py file in you app
just type this in your project urls.py so you don’t make a mess with paths and includes
urlpatterns = [
path('admin/', admin.site.urls),
path("",include('pomofocus.urls'))
]
this is just an example you need to enter your own paths and views
What's your code problem?
According to your code, you have two same paths for the home page, and when you enter http://127.0.0.1:8000/hello/ Django look at the project-level urls.py (in your code djangoProject1/urls.py) and knows that must go in app-level urls.py (in your code practice/urls.py) and in this file, you have two same paths, and Django uses the first one so you get write page because first one is related to hello view and when you type http://127.0.0.1:8000/dashboard/ Django again look at the project-level urls.py and knows that must go in app-level urls.py and in this file again use the first path and you get incorrect HTML file (because both paths are the same and Django always use the first one)
How to fix this?
in project-level url.py set both paths to '' and in app-level set your project-level paths. (in other words, replace paths in app-level and project-level
djangoProject1/urls.py:
import practice.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('practice.urls')),
]
practice/urls.py:
from django.urls import path
from practice import views
urlpatterns = [
path('hello/', views.hello_world, name='hello_world'),
path('dashboard/', views.dashboard, name='dashboard')
]
Your code should be like in
practice/urls.py
from django.urls import path
from practice import views
urlpatterns = [
path(" ", views.hello_world, name='hello_world'),
path("dashboard", views.dashboard, name='dashboard')
]
Project1 urls.py
import practice.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(" ", include('practice.urls')),
]
Say I am creating a Todo app in Django, traditionally I would include a path to my app in the base_project urls.py file. However, today I came across a tutorial where they used a router for this same purpose I've already stated.
Why would I want to use a Router instead of including the path in my urls.py file?
For reference, here is a snip from the tutorial found at https://www.digitalocean.com/community/tutorials/build-a-to-do-application-using-django-and-react
# backend/urls.py
from django.contrib import admin
from django.urls import path, include # add this
from rest_framework import routers # add this
from todo import views # add this
router = routers.DefaultRouter() # add this
router.register(r'todos', views.TodoView, 'todo') # add this
urlpatterns = [
path('admin/', admin.site.urls), path('api/', include(router.urls)) # add this
]
Here backend is the main django project name.
Both works the same. You can add url in the urlpatterns. I had the same situation as yours where in a tutorial they were using routing instead of url patteren but i studied and both works the same.
I'm following a django tutorial that is a little outdated and in the urls.py file of the first app's directory we need to configure where to direct django to for any url starting with 'notes/'.
There are two separate 'apps' inside the project. I'm in the first one, not notes.
This is the code currently. I added include to import statement:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(url(r’^notes/‘, include('notes.urls'))),
]
Inside the urlpatterns object, on the first line, path('admin/', admin.site.urls), comes predefined, but I need to add a redirect such that django goes to a different app, called 'notes' and searches there for its entry point, since all of the urls will begin with ‘notes/’.
The tutorial says to use a regular expression and uses this code:
url(r’^notes/‘, include(notes.urls))
so that any url that starts with 'notes/' should be redirected to this other file notes.urls.
However the predefined ones that currently come out of the box with a django project start with path.
I enclosed my notes/n redirect line in path, but not sure if this is correct. Should I instead directly write:
url(r’^notes/‘, include(notes.urls))
Also, do I need to delete the first line provided?
path('admin/', admin.site.urls),
The tutorial has:
urlpatterns = patterns('',
url(r’^notes/‘, include(notes.urls)),
)
and no admin urls line. It's from 2014 I believe.
Simply do:
path('notes/', include('notes.urls'))
I migrated my Django project to an Ubuntu distant server. I'm using mod_wsgi in order to runserver and I have some questions about default page.
I'm really new in this domain and I apologize if my question is bad or useless ..
When I want to connect to my Django application, I have to write something like that :
http://172.XX.XX.XXX/Home/login/
If I write just :
http://172.XX.XX.XXX
I get :
Page not found
Using the URLconf defined in Etat_civil.urls, Django tried these URL patterns, in this order:
^admin/
^BirthCertificate/
^Identity/
^Accueil/
^Home/
^captcha/
^Mairie/
The current URL, , didn't match any of these.
My question is :
How I can define redirected url in order to write http://172.XX.XX.XXX in my browser and go directly to http://172.XX.XX.XXX/Home/login/ for example ?
This is urls.py file from my project :
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from BirthCertificate import views
from Identity import views
from Accueil import views
from log import views
from Mairie import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^BirthCertificate/', include('BirthCertificate.urls')),
url(r'^Identity/', include('Identity.urls')),
url(r'^Accueil/', include('Accueil.urls')),
url(r'^Home/', include('log.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^Mairie/', include('Mairie.urls')),
]
Because I just want for example, write my IP adress in order to access to my Django application and not write all the time a complete url.
If you need some files (apache2 files, ...) please tell me which one I have to post there.
Include this in views.py file of your any app:
from django.shortcuts import redirect
def some_view(request):
return redirect('/Home/login/')
Suppose the view is in log app then,
Include this in your urls.py:
from log.views import some_view
urlpatterns = [
url(r'^$', some_view,name='index'),
url(r'^admin/', admin.site.urls),
url(r'^BirthCertificate/', include('BirthCertificate.urls')),
url(r'^Identity/', include('Identity.urls')),
url(r'^Accueil/', include('Accueil.urls')),
url(r'^Home/', include('log.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^Mairie/', include('Mairie.urls')),
]
One method is to use RedirectView by making the following changes to your urls.py:
from django.views.generic.base import RedirectView
urlpatterns = [
url(r'^$', RedirectView.as_view(url='/Home'), name='home'),
...
]
You can either do this in Django or Configure from your webserver. Django has redirects app go through https://docs.djangoproject.com/en/1.10/ref/contrib/redirects/ for more info. You just add the source and redirect urls in the redirects section of your django admin.
env:
Django==1.7.8
wagtail==0.8.8
execute "manage.py runserver" to launch the website
I copy a example.mp4 file to /static dir, then can open it in browser#127.0.0.1:8000/statice/example.mp4
but when I insert http://127.0.0.1:8000/statice/example.mp4 to page as a embed component, the server return a 500 error.
then I use another internet url, http://viemo.com/86036070, and wagtail still show a 500 error.
the embed url will be send to /admin/embeds/chooser/upload/
I decide to review the related source code to find out, but...
top urls.py:
urlpatterns = patterns('',
url(r'^django-admin/', include(admin.site.urls)),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^search/', include(wagtailsearch_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url('^sitemap\.xml$', sitemap),
url(r'', include(wagtail_urls)),
)
and admin_usrls:
from django.conf.urls import url
from wagtail.wagtailadmin.forms import PasswordResetForm
from wagtail.wagtailadmin.views import account, chooser, home, pages, tags, userbar, page_privacy
from wagtail.wagtailcore import hooks
urlpatterns += [
url(r'^$', home.home, name='wagtailadmin_home'),
url(r'^failwhale/$', home.error_test, name='wagtailadmin_error_test'),
url(r'^explorer-nav/$', pages.explorer_nav, name='wagtailadmin_explorer_nav'),
url(r'^pages/$', pages.index, name='wagtailadmin_explore_root'),
url(r'^pages/(\d+)/$', pages.index, name='wagtailadmin_explore'),
url(r'^pages/new/(\w+)/(\w+)/(\d+)/$', pages.create, name='wagtailadmin_pages_create'),
url(r'^pages/new/(\w+)/(\w+)/(\d+)/preview/$', pages.preview_on_create, name='wagtailadmin_pages_preview_on_create'),
url(r'^pages/usage/(\w+)/(\w+)/$', pages.content_type_use, name='wagtailadmin_pages_type_use'),
url(r'^pages/(\d+)/edit/$', pages.edit, name='wagtailadmin_pages_edit'),
url(r'^pages/(\d+)/edit/preview/$', pages.preview_on_edit, name='wagtailadmin_pages_preview_on_edit'),
url(r'^pages/preview/$', pages.preview, name='wagtailadmin_pages_preview'),
url(r'^pages/preview_loading/$', pages.preview_loading, name='wagtailadmin_pages_preview_loading'),
url(r'^pages/(\d+)/view_draft/$', pages.view_draft, name='wagtailadmin_pages_view_draft'),
url(r'^pages/(\d+)/add_subpage/$', pages.add_subpage, name='wagtailadmin_pages_add_subpage'),
url(r'^pages/(\d+)/delete/$', pages.delete, name='wagtailadmin_pages_delete'),
url(r'^pages/(\d+)/unpublish/$', pages.unpublish, name='wagtailadmin_pages_unpublish'),
url(r'^pages/search/$', pages.search, name='wagtailadmin_pages_search'),
url(r'^pages/(\d+)/move/$', pages.move_choose_destination, name='wagtailadmin_pages_move'),
url(r'^pages/(\d+)/move/(\d+)/$', pages.move_choose_destination, name='wagtailadmin_pages_move_choose_destination'),
url(r'^pages/(\d+)/move/(\d+)/confirm/$', pages.move_confirm, name='wagtailadmin_pages_move_confirm'),
url(r'^pages/(\d+)/set_position/$', pages.set_page_position, name='wagtailadmin_pages_set_page_position'),
url(r'^pages/(\d+)/copy/$', pages.copy, name='wagtailadmin_pages_copy'),
url(r'^pages/moderation/(\d+)/approve/$', pages.approve_moderation, name='wagtailadmin_pages_approve_moderation'),
url(r'^pages/moderation/(\d+)/reject/$', pages.reject_moderation, name='wagtailadmin_pages_reject_moderation'),
url(r'^pages/moderation/(\d+)/preview/$', pages.preview_for_moderation, name='wagtailadmin_pages_preview_for_moderation'),
url(r'^pages/(\d+)/privacy/$', page_privacy.set_privacy, name='wagtailadmin_pages_set_privacy'),
url(r'^pages/(\d+)/lock/$', pages.lock, name='wagtailadmin_pages_lock'),
url(r'^pages/(\d+)/unlock/$', pages.unlock, name='wagtailadmin_pages_unlock'),
url(r'^choose-page/$', chooser.browse, name='wagtailadmin_choose_page'),
url(r'^choose-page/(\d+)/$', chooser.browse, name='wagtailadmin_choose_page_child'),
url(r'^choose-external-link/$', chooser.external_link, name='wagtailadmin_choose_page_external_link'),
url(r'^choose-email-link/$', chooser.email_link, name='wagtailadmin_choose_page_email_link'),
url(r'^tag-autocomplete/$', tags.autocomplete, name='wagtailadmin_tag_autocomplete'),
# 登陆
url(r'^login/$', account.login, name='wagtailadmin_login'),
url(r'^account/$', account.account, name='wagtailadmin_account'),
url(r'^account/change_password/$', account.change_password, name='wagtailadmin_account_change_password'),
url(r'^account/notification_preferences/$', account.notification_preferences, name='wagtailadmin_account_notification_preferences'),
url(r'^logout/$', account.logout, name='wagtailadmin_logout'),
url(r'^userbar/(\d+)/$', userbar.for_frontend, name='wagtailadmin_userbar_frontend'),
url(r'^userbar/moderation/(\d+)/$', userbar.for_moderation, name='wagtailadmin_userbar_moderation'),
]
# This is here to make sure that 'django.contrib.auth.views.login' is reversed correctly
# It must be placed after 'wagtailadmin_login' to prevent this from being used
urlpatterns += [
url(r'^login/$', 'django.contrib.auth.views.login'),
]
# Import additional urlpatterns from any apps that define a register_admin_urls hook
for fn in hooks.get_hooks('register_admin_urls'):
urls = fn()
if urls:
urlpatterns += urls
I don't find which view /admin/embeds/chooser/upload/ will be routed to. but I am sure there is a view function map to this url, because I add a "print" in django.forms.forms.is_valid method, and it be triggered.
can anybody help me? thanks in advance, and sorry for my D-level english.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
update:
I create a new wagtail project, and write a simple page model, then I insert a embed to the content field, a RichTextField, the server return OK-200, but nothing was insert to content in edit panel.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
update:
the view for the path is wagtailembeds.views.chooser.chooser_upload. I will check it.
thanks all.