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.
Related
I am trying to change django default homepage with my custom design, but it's not working. I Try a lot but still the same issue, I am not Understanding what is the issue, it's working perfect on my local server, But I am implementing Django app on Live server, Please let me know where I am mistaking.
here is my django default urls.py file...
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from django.conf import settings
from django.urls import path, include
from django.conf.urls.static import static
from . import views
urlpatterns = [
path('mainadmin/admin/', admin.site.urls),
url(r'ckeditor/', include('ckeditor_uploader.urls')),
url(r'^myadmin/', include('myadmin.urls')),
url('', include('homepanel.urls')),
path('', views.index, name='index'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
There are many ways you can change the admin panel of the your django website.
here is the article which will tell you how can you implement that
I am completely new in Django and currently learning it from Udemy course.
First, I created my Django project and then created my application. I have inserted my application on settings.py in INSTALLED_APPS of project folder and then re run the the server as well.
Now, I have created a view in my application folder and want to use that in url.py of project folder. I could not able to import it as its showing that package not found.
Please see my program structure below:
I have written the code on pycharm IDE. My code in url.py is below
from django.contrib import admin
from django.urls import path
from firstApp import views
urlpatterns = [
path('^$', views.index, name='index'),
path('admin/', admin.site.urls),
]
Try changing path('^$', views.index, name='index') to path('^$', include('firstApp.urls'), namespace='index')
And you need to import include from django.urls
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
I am having a hell of a time getting the reverse function to work with my DRF implementation. Here is my urls.py, which i would assume the following reverse to work for:
urls.py
from django.conf.urls import url, include
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'compressors', views.CompressorViewSet, base_name='compressors')
from django.urls import include, path
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls, namespace='router')),
url(r'^api-auth/', include('rest_framework.urls',
namespace='rest_framework'))
]
And then
reverse('router:compressors', kwargs)
What am I missing? I am getting a app_name not provided, which when added to this file does not help. Is there a way for me to put an app_name to the router?
Turns out its just hard to find exactly how the use basename, I did not append '-list' to my reverse. It should have been the following, hope this helps others:
reverse('router:compressors-list')
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.