I've been following this tutorial (Django Tutorial Part 3) and I've stumbled across what I'm assuming is a syntax issue between the time this tutorial was written and the new Django releases. My admin page loads just fine.
Code Block in Question So in the picture linked, that is the area that I am having trouble with from the tutorial. I think my "mysite/urls.py" file is fine, but the "polls/urls.py" file is where I'm not sure what to put.
Below is what mysite/urls.py looks like:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
And here is what my polls/urls.py looks like:
from django.urls import path
from . import views
urlpatterns = [
path(**??? not sure what to put here**, views.index, name='index'),
]
but the polls/urls.py file is where I'm not sure what to put.
The empty string (''), so:
urlpatterns [
# ↓↓ empty string
path('', views.index, name='index')
]
We can inspect how the path is compiled to a regex with:
>>> from django.urls import path
>>> pt = path('', lambda x: x)
>>> pt.pattern.regex
re.compile('^$')
The pattern is thus compiled to a regex ^$, which is the same as the regex for the url pattern in the documentation.
Related
I just started learning Django and I am a bit confused. I'm following the official documentation and this is the code:
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
polls/views.py
from django.http import HttpResponse
from django.conf import settings
from django.shortcuts import render
from django.views import View
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I am mainly confused that if I add more functions in polls/views.py, how will using inclde('polls.urls') know that I wish to call the index function or some other function?
Link for the documentation page that I am following: https://docs.djangoproject.com/en/3.2/intro/tutorial01/
Essentially it constructs a list of paths (strings) that it tries to build up and then find which you wanted
so paths in polls for "" and foobar would lead django to try to match up to the following urls
polls/
polls/foobar
The full path for the views.index is 'polls/' + '' and thus 'polls/', so it appends the two.
If you make an extra path:
# polls/urls.py
urlpatterns = [
path('', views.index, name='index')
path('foo/', views.other_view, name='other-view')
]
then that path pattern will be 'polls/'+'foo/' = 'polls/foo/', so it will disambiguate between the two.
If you thus visit polls/, then the first path will match and thus "fire" index. If you visit polls/foo, then the other-view will "fire".
I don't know why this problem happened.enter image description here
These errors occur when you read a book and follow it. I don't know what to do. Give me a hand.
Source Code
This is my fistsite.py source code.
fistsite.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
enter code here
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
After changing the source code above
now fistsite.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('/polls', views.polls, name='polls'),
path('/admin', views.admin, name='admin')
]
But there was an error.enter image description here
my projcetenter image description here
Assuming that you have Django version > 2.0, you should add to your urls.py file something like this:
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('/polls', views.polls, name='polls'),
path('/admin', views.admin, name='admin')
]
So as shown in the error page, your Django project tried to go to localhost:8000/admin/ and localhost:8000/polls/ but the site you are trying to access in localhost:8000/ . You haven't defined any url pattern for capturing this scenario. What you can do is in your urls.py, add this line under urlpatterns :
urlpatterns=[
path('', views.index, name='index'),
...............
]
This works if you are using Django version>2.0
If you are using Django version<2.0 then you can add this:
urlpatterns=[
url(r'^$', views.index, name='index'),
...............
]
If you want to access you "polls". After change your fistsite.pytry this
url = "localhost:8000/polls/
and you are giving only localhost:8000 if did not create any app then this url will work.
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/ [name='index']
admin/
The current path, polls/, didn't match any of these.
My code is...
from (app)polls urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
from (project)mysite urls.py
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
from views.py project file
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world, you are at the polls index.")
If you want your view index as a home view, the first page to be rendered on the address localhost:port/.
Then,
In polls.urls.py should be (the same as yours):
...
path('', views.index, name='index'),
...
And in project.urls.py
...
path('', include('polls.urls')),
...
It should work.
An extra.
Now if you want to render on the address, lets say, localhost:port/polls
Then your path pattern inside you app.url, (not project.url), would be:
path('polls/', polls_view, name='polls')
Hope it helps.
Since you are accessing localhost:8000, the search path is empty string '' and in your project urls, there are only two pattern for polls and admin. That's why you are getting the error. So you need to access localhost:8000/polls to get the result from index view of polls app.
I am a beginner and I have been troubled for a long time with the same problem.
The key to the problem is the directory:
mysite/
mysite/
polls/
....
I suspect that you have created a new file(urls.py) in mysite(The outermost layer of folders) directory and it's invalid. You should modify the file: mysite/mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
So I am following a Django tutorial and I have reached the stage where I add my own URL patterns to the urls.py file.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url((r'^webapp/', include('webapp.urls'))),
url(r'^admin/', admin.site.urls),
]
the problem is that when I run the server Django doesnt even search for the pattern. I'm not sure what I'm doing wrong here
webapp.urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Make sure the directory structure is:
app
app
urls.py
webapp
urls.py
You write about a file named webapp.urls.py that is unusable by django
You pass wrong arguments for url().
Wrong:
url((r'^webapp/', include('webapp.urls')))
Right:
url(r'^webapp/', include('webapp.urls'))
I have this code in one file called urls.py
from django.conf.urls import url
from django.contrib import admin
from myblog import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', urls),
]
Im trying to redirect to another file in a module called myblog which contains another file called urls.py with the following code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
my views file which contains post_list method is as follows:
from django.shortcuts import render
def post_list(request):
return render(request,'myblog/post_list.html', {})
The result is that i keep getting the following error message:
module 'myblog.urls' from 'path\to\python\file\urls.py' is not a callable or a dot-notation path
Can anyone please explain to me where i am getting it wrong. I am new to django and python and i am using a tutorial from DjangoGirls.com. They use Django 1.8 and i have Django 1.9 installed.
Looking at the tutorial, uou can see that you have forgotten to use include.
First you have to add the import
from django.conf.urls import include, url
Then change the url pattern to:
url(r'', include('blog.urls')),
Be careful that you are using the correct module name - you have myblog, but the tutorial has blog.
You need to include the urls.py to link them
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(urls, namespace='MyOtherapp')),
]
Note: I've also added a namespace here, its not required, it just helps when your using the url template tag.