I've recently completed the Django Tutorial and am now working on my own web app. The problem I am having is setting up the URLConf for this application. I still don't fully understand the RegEx matching in order to link different pages to each other (or maybe I'm just forgetting something simple??)
I'm trying to setup the URLConf in a way such that when I click a button on each page, it will travel to the next page (there are 5 total).
Here is what it should look like
Page 0 (http://127.0.0.1:8000/)
Page 1 (http://127.0.0.1:8000/page1/)
And continue in this pattern (http://127.0.0.1:8000/page2/, http://127.0.0.1:8000/page3/, http://127.0.0.1:8000/page4/)
When I click Next Page on Page 0, it goes to Page 1. When I click Submit on Page 1, the URL slug page1 changes to page2 but still displays the same html page.
Here are my two urls.py files:
"""qfdjango URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
# builds URLS for all across the site
# decoupled from mainsite.urls
urlpatterns = patterns('',
url(r'^', include('mainsite.urls')),
url(r'^someOtherPage/', include('mainsite.urls')),
url(r'^anotherPage/', include('mainsite.urls')),
url(r'^page1/', include('mainsite.urls')),
url(r'^page2/', include('mainsite.urls')),
url(r'^page3/', include('mainsite.urls')),
url(r'^page4/', include('mainsite.urls')),
url(r'^admin/', include(admin.site.urls)),
)
"""qfdjango URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import patterns, include, url
# these patterns are for specific sections of the site
# the 'primary' URLs are located in qfdjango.urls
# URL design of an app is specific to the app,
# not the whole Django project
urlpatterns = patterns('mainsite.views',
url(r'^$', 'index'),
url(r'$', 'page_1'),
url(r'$', 'page_2'),
url(r'$', 'page_3'),
url(r'$', 'page_4'),
)
It seems you have defined the urls in a wrong way.
It should be something like:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^', include('mainsite.urls')), # include your app urls once
url(r'^admin/', include(admin.site.urls)),
)
Then in app's urls.py file, urls should be defined like:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('mainsite.views',
url(r'^$', 'index'),
url(r'^someOtherPage/$', 'someOtherPage'),
url(r'^anotherPage/$', 'anotherPage'),
url(r'page1/$', 'page_1'), # page 1 url
url(r'page2/$', 'page_2'), # page 2 url
url(r'page3/$', 'page_3'), # page 3 url
url(r'page4/$', 'page_4'), # page 4 url
)
Related
The included URLconf '<module 'myapp.urls' from 'C:\\Users\\Hp\\Desktop\\Python\\Django Projects\\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.
This is the error that I am getting while building my django app.
Here is urls.py from myapp -
from django.urls import path
from . import views
urlpatterns=[
path('', views.index, name='index')
]
Here is views.py -
from django.shortcuts import render
from django import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('<h1>Hey, Welcome</h1>')
this is from urls.py from myproject-
"""myproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
try to edit your view file like the following :
from django.shortcuts import render
#this is new
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('<h1>Hey, Welcome</h1>')
I am new in using Django and I am having some problems. When I run my server 'runserver' it shows 404 page not found. My project directory name is 'mysite' and my app name is 'webapp'. The problem I think is in 'urls' file. I have also put my app name in INSTALLED_APPS under the settings section.
This is the code in mysite/urls.py file:
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('webapp', include("webapp.urls")),
]
And this is the code in webapp/urls.py file:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = "index"),
]
I am using the latest Django version 2.0.5, I have tried to look up for this error but most of them seems to be of the older versions of Django.
I would appreciate any help, in this problem.
In your webapp/urls.py, you have to specify app name like
app_name = "webapp"
mysite/urls
urlpatterns =
[
path('webapp/'), include() )
]
Note: parent urls must contain trailing slash.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('webapp/', include('webapp.urls')),
]
You forgot to add backslash after webapp in path "webapp/" which would help to redirect to further methods in views.
As nothing has been defined for your root URL,it is thus empty and the 404 error occurs.To solve it,simply type the path to your view after the local server URL in your address bar.i.e "127.0.0.1:8000/webapp" or use "localhost/name_of_url"
I'm trying to set up django-registration-redux, but when I set up the
url(r'^accounts/', include('registration.backends.default.urls')),
in the urls.py file and I try to access any page I get the following error message:
ModuleNotFoundError
No module named 'django.urls'
I have checked the manual several times and everything is in order. What is missing? Where is my mistake?
urls.py file
"""p110 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import include, url
from django.contrib import admin
from boletin import views
from .views import about
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^about/$', about, name='about'),
url(r'^accounts/', include('registration.backends.default.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
The latest version of django-registration-redux requires Django 1.11+. If you are using an earlier version of Django, then you could use django-registration-redux 1.9, which supports Django 1.8+.
Note that you should really be upgrading to Django 1.11 or newer. Django 1.9 and 1.10 are no longer supported, and long term support for Django 1.8 ends in April 2018.
I'm trying out Python Django. With eclipse pyDev. But I'm unable to simply get my first url to display.
This urls.py is from the Cr package.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
This urlspy is from the Crowd package.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'Crowd/', include('Cr.urls'))
]
So what I've understood from this the Crowd package is the "main" webservice(?), and by using include I can whenever the regular expression matches Crowd, will pass it on to the other urls.py(Cr). But the debugger passes:
Using the URLconf defined in Crowd.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, crowd, didn't match any of these.
my views.py file
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse('<h1>Hello World!</h1>')
I tried to access it with http://127.0.0.1:8000/Crowd
Below is an image of the project folder.
Can we see your settings.py file? There is a place in there where you define your project's url file. I'm assuming it's right now either not there or it's pointing to the wrong place, because Django can't find your urls.py file.
For example, in my settings.py file for one of my projects, I have:
ROOT_URLCONF = 'Freya.urls'
Where "Freya" is my project name
Just for reference, not that I know this will solve your problem, this is what (part of) my urls.py file looks like for one of my projects:
from django.conf.urls import patterns, url
import views
urlpatterns = patterns(
'',
url(r'^$', views.index, name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
)
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'Crowd/', include('Cr.urls'))
]
just use this urls.py file
I have just started with django now and was configuring urls. I am able to map different urls like /posts, /posts/create etc. Somehow I am not able to configure root url, I am not sure what wrong I am doing. Here is my configuration :
urlpatterns = [
# Examples:
url(r'',homeViews.posts),
# url(r'^blog/', include('blog.urls')),
url(r'^posts/',homeViews.posts),
url(r'^createPost/',homeViews.createPost),
url(r'^createUser/',userViews.createUser),
url(r'^post/(?P<title>[a-z,A-Z]+)/$',homeViews.post),
]`
Cheers!
here is an example, my friend.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'home.views.home',name='index'),
url(r'^contactanos/$', 'home.views.contacto',name='contacto'),
url(r'^post/$', 'home.views.blog',name='blog')
]