change Django default URL - python

first sorry for my bad English
When I run my server, I want to Django redirect me to http://127.0.0.1:8000/home instead of http://127.0.0.1:8000.
what should I do?

Try to define urlpatterns in urls.py file like this:
urlpatterns = [
path('', RedirectView.as_view(url='home', permanent=False),
name='redirect'),
path('home/', MainView.as_view(), name='index'),
]
I hope it will work for you.

Related

Reverse for 'hobbieswithCSS.html' not found. 'hobbieswithCSS.html' is not a valid view function or pattern name

I am trying to attach hobbieswithCSS.html file to my website, while using Django. I am a beginner when it comes to Django, so I have naturally came across some problems (in the title) like this.
I have this anchor tag on my homepage -
My Hobbies
I have this view in my views.py file -
def hobbieswithCSS(request):
return render(request,'basic_app/hobbieswithCSS.html')
I think, that the main problem will be with the urlpatterns in urls.py file, because I am not sure how to set it up. These are my urlpatterns -
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^basic_app/',include('basic_app.urls')),
url(r'^logout/$',views.user_logout,name='logout'),
url(r'^$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
Could anybody please tell me, how could I change the code in order to make that hobbieswithCSS.html file display, when I am trying to run it on my server?
It must be:
My Hobbies
Without basic_app.
Also you have the same path for index and hobbies . You have to change your url path:
urlpatterns = [
url(r'^$', views.index, name='index'),
...
url(r'^hobbies/$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
in your template update to
My Hobbies

Django : Cannot resolve the URLs pattern : Always Index page content is displayed

I am learning Django. I build an app in which I am setting up the URL patterns but in all cases it's displaying the data for the index page.
appTwo urls.py File :
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.users, name='users'),
url(r'^$', views.help, name='help'),
]
MyProject urls.py file :
urlpatterns = [
url(r'^$', views.index, name='index'),
url('admin/', admin.site.urls),
url(r'^users/', include('appTwo.urls')),
url(r'^help/', include('appTwo.urls')),
]
If I call /users or /help, the browser display the data for index file only.
Is there something with a regex that I am doing wrong?
You're setting your urlpatterns in a wrong way. First of all, in you project's urls.py file you are saying that if a request is sent to /users or /help, then Django should look into your appTwo.urls. When Django gets there, it finds that the urlpatters are set so that everything that's empty after any of the aforementioned urls must be handled by the views.index, views.users and views.help. But as views.index is the first one in the list, then all the request end up being handled by that view.
By the way, if you're using Django >= 2.0 you no longer need to use the url() function, but the path() one instead, for which you can declare the paths as simple strings rather than regular expressions.
You should have something as follows:
MyProject/urls.py
from django.urls import path
urlpatterns = [
path('', include('appTwo.urls')),
path('admin/', admin.site.urls)
]
MyApp/urls.py
from django.urls import path
urlpatterns = [
path('', views.index, name='index'),
path('users/', views.users, name='users'),
path('help/', views.help, name='help'),
]
when django reaches here (in your apps url) while resolving the urls, see all three urls have empty regex as first param (so the first one will be selected), And as you see its index then you always show index page. You need to differentiate the urls. Something like this.
urlpatterns = [
url(r'^users/$', views.users, name='users'),
url(r'^help/$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
I think you also cant write url index with empty regex at top of other urls, otherwise it will always be selected.

Django Include() in urls.py with two apps

I believe this is a simple question but I am having a hard time figuring out why this is not working.
I have a django project and I've added a second app (sales). Prior to the second app, my urls.py simply routed everything to the first app (chart) with the following:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('chart.urls')),
]
and it worked fine.
I have read the docs over and over a looked at many tutorials, so my impression is that I can simply amend the urls.py to include:
urlpatterns = [
path('admin/', admin.site.urls),
path('sales/', include('sales.urls')),
path('', include('chart.urls')),
]
and it should first look for a url with sales/ and if it finds that then it should route it to sales.urls, and if it doesn't find that then move on and route it to chart.urls. However, when I load this and type in 127.0.0.1:8000/sales, it returns the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/sales/
Raised by: chart.views.index
which tells me that it is not routing my sales/ url to sales.urls but to chart.urls. When I change path('', include('chart.urls')), to path('', include('sales.urls')), it does route it to sales.urls so I know that my sales.urls file is set up correctly.
I know this is probably an easy question but I cannot figure it out with anything I've read. Any help is appreciated.
chart.urls:
from django.urls import path, re_path
from . import views
urlpatterns = [
path('dashboard/', views.chart, name='dashboard'),
path('', views.index, name='index', kwargs={'pagename': ''}),
path('<str:pagename>/', views.index, name='index'),
]
sales.urls:
from django.urls import path
from . import views
urlpatterns = [
path('sales/', views.sales, name='Sales Dashboard'),
]
I think the correct url for the sales page in your case would be http://127.0.0.1:8000/sales/sales/.
This is because sales/ is then looking in the included sales.urls and not finding any urls at the root / (the only url included there is at sales/ (within sales/) so will be sales/sales/
Reply to an OLD question but may be useful for others...
from django.urls import path, include
dont for get to import include
Do this:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('chart.urls')),
url(r'^sales/', include('sales.urls')),
]
you are not doing it properly since you are not telling your app where go once it is loaded .this r'^' to go directly to chart.url url(r'^', include('chart.urls')),

Having issues with Django URL mapping

I am trying Django for an upcoming project and going through a tutorial and running into issue with defining URL paths. Below is the project structure showing the urls.py at the project root.
The urls.py in my timer package is pretty simple:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
However when I launch the application and navigate to localhost:8080/timer/ I am getting a 404. Any suggestions what I should be looking at?
You can try to change URLs in the settings of the whole project like this:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^timer/$', include('timer.urls')),
]
As for the timer, the URLs will be like this:
urlpatterns = [
url(r'^$', views.index, name="index"),
]
Don't forget about r's in the url().
It turns out that I created the urls.py under a wrong folder. I misunderstood the instructions and created the urls.py in the same folder as manage.py. Once I add the new url pattern in the file projectdb/projectgb/urls.py, the issue is fixed. Thanks.

Trying to trace a circular import error in Django

I understand circular import error has been asked about a lot but after going through these questions I haven't been able to solve my issue. When I try to run my server in Django its giving me this error message:
The included URLconf module 'accounts_app' from path\to\myproject\__init__.py does not appear to have any patterns in it. if you see valid patterns in the file then the issue is probably caused by a circular import.
The issue started when i added a new app which has a urls.py like the following
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^signin$', views.signin, name='signin'),
url(r'^signout$', views.signout, name='signout'),
url(r'^signup$', views.signup, name='signup'),
]
My project urls.py has a line which points to the app and looks like the following code
urlpatterns = [
url(r'^accounts/', include('accounts_app')),
]
My view looks like the following:
from django.shortcuts import render
from django.http import HttpResponse
def signin(request):
return HttpResponse("<p>This the signin view</p>")
def signout(request):
return HttpResponse("<p>This the signout view</p>")
def signup(request):
return HttpResponse("<p>This the signup view</p>")
Can anyone please help me identify were I could possibly be going wrong.
for those who have the same error but still hasn't debugged their code, also check how you typed "urlpatterns"
having it mistyped or with dash/underscore will result to the same error
Try changing
urlpatterns = [
url(r'^accounts/', include('accounts_app')),
]
to
urlpatterns = [
url(r'^accounts/', include('accounts_app.urls')), # add .urls after app name
]
Those habitual with CamelCased names may face the error as well.
urlpatterns has to be typed exactly as 'urlpatterns'
This will show you error -
urlPatterns = [
path('', views.index, name='index'),
Error -
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'polls.urls' from '...\\polls\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
However, fixing the CamelCase will work -
urlpatterns = [
path('', views.index, name='index'),
]
After 1 hour search it appears that it's wrong speelling it should be : urlpatterns
urlpatterns = [
path('', views.index, name="index")
]
In my case, I got this error because I called the reverse function for a url that required a slug parameter without placing the correct parameter in it.
Once I fixed the reverse function, it was resolved.
In my case I was getting error because I was giving wrong path of dir containing urls. So I changed this
urlpatterns = [
url(r'^user/', include('core.urls'))
]
to this
urlpatterns = [
url(r'^user/', include('core.urls.api'))
]
In my case i was getting this error because i was typing urlpatterns in urls.py to urlpattern.
This error also appears if SomeView doesn't exist in views.py and you do this:
from someapp import views
urlpatterns = [
path('api/someurl/', views.SomeView.as_view(), name="someview"),
]
So make sure all Views you use in urls.py exist in views.py
In my case, I was getting
/SLMS_APP1/urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
I made a typo in 'urlpatter'
urlpatter = [
path('',views.index, name='index'),
]
where in correct spelling has to be 'urlpatterns'
urlpatterns = [
path('',views.profile, name='profile'),
]
this error basically occurs when you have, include the app in the main urls.py file and haven't declared urlpattern= [] in-app file.
In my case I changed "reverse" for "reverse_lazy" on my success_url and magially it works.
I changed this...
app_name='users'
urlpatterns=(
path('signup/', SignupView.as_view(),name='signup')
)
to this...
app_name='users'
urlpatterns=(
path('signup/', SignupView.as_view(),name='signup'),
)
notice the last COMMA

Categories

Resources