I just started learning Django and got the following error:
[![The site doesn't see URL and then Error 404][1]][1]
urls.py - site
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('webexample/', include ('webexample.urls')),
]
urls.py - webexample
from django.urls import path
from . import views
urlpatterns = [
path(r'^$', views.index, name='index'),
]
views.py - webexample
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h3>This page isn't set up yet...</h3>")
Error photo
change path to this to work for the current url.
path(r'', views.index, name='index')
You could do this
path('index/', views.index, name='index'),
or
re_path(r'^index/$', views.index, name='index'),
to know more about this, you can the django doc https://docs.djangoproject.com/en/3.0/ref/urls/
Related
I need help tried to run server for Django and got this error, The included URLconf '<module 'myapp.urls' from 'C:\Users\user\Documents\Django Tutorial\myproject\myapp\urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.
for urls.py in myproject is:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
for myapp views.py is:
from django.shortcuts import render
from django.http import HttpResponse
def index(request): return HttpResponse('hello')
For myapp urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
So I have code below that is in django 1.8
from django.conf.urls import patterns, url
from account import views
from django.contrib.auth import views as auth_views
urlpatterns = patterns('',
url(r'^$', views.index, name='profile'),
url(r'^api/get_users/(?P<term>.*)', views.get_users),
url(r'^leaderboard/(?P<board_type>.*)', views.leaderboard),
url(r'^admintools/(?P<action>.*)', views.admintools),
)
I modified it to django 2.2
from django.conf.urls import url
from . import views
from django.urls import re_path,path
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', views.index, name='profile'),
path('api/get_users/(?P<term>.*)', views.get_users),
path('leaderboard/(?P<board_type>.*)', views.leaderboard),
path('admintools/(?P<action>.*)', views.admintools),
]
I get the error The current path account/admintools, didn't match any of these
"one of the easy solutions" to this problem is, use re_path(...) instead of path()
from django.urls import re_path
from account import views
urlpatterns = [
re_path(r'^$', views.index, name='profile'),
re_path(r'^api/get_users/(?P<term>.*)', views.get_users),
re_path(r'^leaderboard/(?P<board_type>.*)', views.leaderboard),
re_path(r'^admintools/(?P<action>.*)', views.admintools),
]
The re_path(...) function will do the same thing as the Django url(...) did.
I'm learning Django,and I followed a guidebook.I want to change the default Django page into my own one.It has took me 2 hours to solve this,and nothing worked.
The project is called learning_log,and the app is called learning_logs.
Here's what I'm trying to do:
1.add module learning_logs.urls:
"""
Definition of urls for learning_log.
"""
from datetime import datetime
from django.urls import path
from django.contrib import admin
from django.contrib.auth.views import LoginView, LogoutView
from app import forms, views
#added
from django.conf.urls import include, url
import learning_logs.views
from django.urls import path,re_path
app_name='learning_logs'
urlpatterns =[
path('', views.home, name='home'),
path('contact/', views.contact, name='contact'),
path('about/', views.about, name='about'),
path('login/',
LoginView.as_view
(
template_name='app/login.html',
authentication_form=forms.BootstrapAuthenticationForm,
extra_context=
{
'title': 'Log in',
'year' : datetime.now().year,
}
),
name='login'),
path('logout/', LogoutView.as_view(next_page='/'), name='logout'),
path('admin/', admin.site.urls),
#added
re_path(r'',include('learning_logs.urls',namespace='learning_logs'))
]
To include the module,I've tried:
url(r'',include('learning_logs.urls',namespacec='learning_logs'))
path('',include('learning_logs.urls',namespacec='learning_logs'))
path('',include('learning_logs.urls'))
path('',learning_logs.urls)
path('',learning_logs.views)
But none of them worked.
2.Create urls.py in learning_logs.here's the code:
"""define learning_logs's url mode"""
from django.urls import path,re_path
from . import views
from django.conf.urls import include,url
urlpatterns=[
#homepageļ¼
re_path(r'(?P^$)',views.index,name='index')]
#path('',include('learning_logs.views.index'))]
As you can see,I also tried many times.
3.Write views.py in learning_logs
from django.shortcuts import render
# Create your views here.
def index(request):
"""homepage of learning logs"""
return render(request,'learning_logs/index.html')
4.write HTML document in learning_logs/templates/learning_logs/index.html
code skipped,for it never came up.
Here is my document tree:
tree
What I want is that show my own homepage at localhost:xxxxx/
How to solve this?
it's simple. just change this
urlpatterns =[
path('', views.home, name='home'),
to this
urlpatterns =[
path('', learning_logs.views.index, name='home'), #correct path to your index view
I don't understand what you're trying to do, why don't you just use:
urlpatterns =[
path('', views.home, name='home'),
And change the name so it directs you to that home page instead?
E.g.
urlpatterns =[
path('', views.learning_logs, name='learning logs'),
Also, since learning logs IS in the views file you shouldn't use
path('',learning_logs.views)
Does that make sense?
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.
I am trying to return different templates for different urls (having just one app), so basically I want to return:
One template for:http://127.0.0.1:8000/projects/
Another template for:http://127.0.0.1:8000/formpage/
I have the project urls.py:
from django.conf.urls import url,include
from django.contrib import admin
from appone import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
#home page
url(r'^$', views.index, name='index'),
url(r'^projects/', include('appone.urls')),
url(r'^formpage/', include('appone.urls')),
]
And the app urls.py:
from django.conf.urls import url,include
from django.http import HttpResponse
from . import views
urlpatterns = [
url(r'^$', views.projs, name='proj'),
url(r'^$', views.form_view, name='form_view')
]
I have the views and templates , that are good, but I do not understand how can I return them based on the url, because for the moment I return the first view from app urls.py, for both urls.
Make separate view functions for both urls (projs and form_view).
Then in projects/urls.py
urlpatterns = [
...
url(r'^projects/', views.projs, name='proj'),
url(r'^formpage/', views.form_view, name='form_view')
...
]`
Or, if you want to have separate urls.py file
projects/urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
#home page
url(r'^$', views.index, name='index'),
url(r'^', include('appone.urls')),
]`
appone/urls.py
urlpatterns = [
url(r'^projects/', views.projs, name='proj'),
url(r'^formpage/', views.form_view, name='form_view')
]`
Do you really need the second urls page? You could just set your first urls.py as:
from django.conf.urls import url,include
from django.contrib import admin
from appone import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
#home page
url(r'^$', views.index, name='index'),
url(r'^projects/', views.projs, name='proj'),
url(r'^formpage/', views.form_view, name='form_view'),
]