I'm totally new to Django, and I'm trying to understand how does it work (I'm more used to PHP and Spring frameworks.
I have a project called testrun and inside it an app called graphs, so my views.py looks like:
#!/usr/bin/python
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, World. You're at the graphs index.")
then, in graphs/urls.py:
from django.conf.urls import patterns, url, include
from graphs import views
urlpatterns = patterns(
url(r'^$', views.index, name='index'),
)
finally, at testrun/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'testrun.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^graphs/', include('graphs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
However, when I try to access http://127.0.0.1:8000/graphs/ I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/graphs/
Using the URLconf defined in testrun.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, graphs/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
What am I doing wrong that I can't get that simple message to be displayed in the browser?
To expand on my comment, the first argument to patterns() function is
a prefix to apply to each view function
You can find more information here:
https://docs.djangoproject.com/en/dev/topics/http/urls/#syntax-of-the-urlpatterns-variable
Therefore in graphs/urls.py you need to fix the patterns call like so:
urlpatterns = patterns('', # <-- note the `'',`
url(r'^$', views.index, name='index'),
)
Related
Okay... let's try to explain things clearly. I've used Python Django to create a dynamic webpage/web-app. After completing the website I have published it using DigitalOcean and have successfully attached my purchased domain name to the name server of DigitalOcean. When I access my website, ordinanceservices.com, i get an error 404; however, if I type ordinanceservices.com/home it works as it should and displays the home page. How, by editing the python files, can I have it to where ordinanceservices.com will display the home page as opposed to error 404? I feel like there's something that I am doing that is fundamentally wrong regarding my .urls structure and thus a rewrite/redirect in the nginx config should not be necessary.
Here is the specific error:
Page not found (404)
Request Method: GET
Request URL: http://ordinanceservices.com/
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
^admin/
^ ^home/ [name='home']
^ ^contact/ [name='contact']
^ ^services/ [name='services']
The current URL, , didn't match any of these.
I somewhat understand what is happening here though I do not know how to fix this. Next I will provide my .urls files for each folder that contains such:
/django_project urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
)
/company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
/company views.py
from django.shortcuts import render
def home(request):
return render(request, 'company/home.html')
def contact(request):
return render(request, 'company/contact.html')
def services(request):
return render(request, 'company/services.html')
What I am aiming to do, without needing to redirect the main URL using the nginx config files to do so, is to edit my urls and views structure of my Python files to ensure that the normal URL, ordinanceservices.com, will actually display a page; preferably the home page of my webpage.
I have a hunch that it has to do with the fact that I do not have a views.index for the r'^admin/' to reach to. I am not certain but I have been trying to figure this out for hours. Does anyone have a clue what I can do to fix this?
You haven't defined anything at the root url. Add one more line to your company urls.py so it becomes
//company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
First of all, check if you have added www.yourdomain.com or yourdomain.com to ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com'] in your settings.py
and then in your company/urls.py do this
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^services/$', views.services, name='services'),
]
and in your main urls.py add this code
from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
]
I'm stuck with a Django project, I tried to add another app called login to make a login page but for some reason the page just redirects to the homepage except for the admin page
For example: 127.0.0.1:8000 will go to the homepage but 127.0.0.1:8000/login will also display the homepage even though I linked another template to it.
Here is my code:
main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('portal.urls')),
url(r'^login/', include('login.urls')),
]
login urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/', views.index, name="login"),
]
login views.py
from django.shortcuts import render
def index(request):
return render(request, 'login/login.html')
portal urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.index, name="portal"),
]
I see 2 problems here:
As #DanielRoseman mentioned above, the regular expression ^ matches anything, so you should change it to ^$.
When you use an include, the rest of the path after what the include matched is passed to the included pattern. You’ll want to use ^$ in your login urls.py too.
You don't terminate the portal index URL, so it matches everything. It should be:
url(r'^$', views.index, name="portal"),
In addition, if the regex is login/$ but you enter http ://server/login, then it won't match wheras http://server/login/ will.
You could try changing the regex to login/*$, which will match any number (even zero) / on the end of the url.
So http: //server/login, http: //server/login/, http: //server/login//// would all match.
Or if you want to be specific, login/{0,1}$ might work (though that regex syntax is from memory!)
I'm going through the polls Django tutorial and have searched for answers to this. So far:
I made sure I'm using the right directories. The main urls.py is in mysite/mysite/, the polls urls.py is in mysite/polls/urls.py.
Tried adding 'polls' to INSTALLED_APPS in the settings.py of mysite/mysite.
Am making sure that I am requesting 127.0.0.1:8000/polls and not 127.0.0.1:8000
I am using Python 3.4 and Django 1.9, same as the tutorial.
I am still receiving this message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, polls, didn't match any of these.
mysite/mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^%', views.index, name='index'),
]
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world")
In your polls/urls.py
change url(r'^%', views.index, name='index') to url(r'^$', views.index, name='index').
Also in your urls.py, you have route for 127.0.0.1:8000/polls/ and you request for 127.0.0.1:8000/polls . Notice the trailing slash.
So you should request to 127.0.0.1:8000/polls/ .
Add APPEND_SLASH = True in your settings.py file so that it would redirect 127.0.0.1:8000/polls to 127.0.0.1:8000/polls/ .
So i tried your Index view with your urls.py.
It works, the only thing different is in your "Request URL, you must request for - 127.0.0.1:8000/polls/%
changing your urls to
url('^/%', index) - polls URLs.py
url('^polls', include('polls.urls')) - project URLs
This will work.
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'm trying to create a site using Django. I have the index view working, however I want to create a simple custom view and map to it but I am unable to. I'm getting a 404 error.
The app inside my project is called emails.
Here are the files:
base/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^emails/$', include('emails.urls')),
)
emails/urls.py
from django.conf.urls import patterns, include, url
from emails import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^custom/$', views.custom, name='custom'),
)
emails/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world, you're at the index.")
def custom(request):
return HttpResponse("This is a custom view.")
Here is the 404:
Using the URLconf defined in crm.urls, Django tried these URL patterns, in this order:
^admin/
^/emails/$
The current URL, emails/custom, didn't match any of these.
When I visit localhost:8000/emails/ - I see the index view. However localhost:8000/emails/custom/ is returning a 404 error. Any help would be appreciated!
This url(r'^emails/$', include('emails.urls')), should be without the dollar sign $:
url(r'^emails/', include('emails.urls')),
You don't want to end your path after emails, so don't end the string with the regex character $. Let it be able to be continued in the other urls.py file