I have an error in django views.py file, httpresponse - django

This is my views.py file, in which i have create a small function.
from django.shortcuts import render, HttpResponse
>
> # Create your views here.
>
>
> def Index(request):
> return HttpResponse("Danial")
This is my urls.py file
from django.contrib import admin
from django.urls import path
from api.views import Index
urlpatterns = [
path('admin/', admin.site.urls),
path('', Index),
]
This is Error
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/It%20is%20working...!
Using the URLconf defined in firstProject.urls, Django tried these URL patterns, in this order:
admin/
The current path, It is working...!, 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.

Not sure what can cause the problem but I see some mistakes in your code:
Commonly used is to name methods with first lowercase charracter - first charracter uppervase is used ot name classes, so rename your method to index().
urlpatterns in urls.py is a list so you shouldn't have that comma behind last member of list.
And just question: your app name is api? That is also sort of keyword and I wouldn't use it as an app name. If your app has other name, this can cause the problem so replace 'api' with your app name - with proper path in your folder structure.

Related

Page Not Found 404 Django & Python

I am having the following error
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in Decoder.urls, Django tried these URL patterns, in this order:
form.html [name='form1']
hl7 [name='hl7']
The empty path 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.
Its my first time writing code using Django
`from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('hl7rest.urls')),
]`
and this other file
from . import views
from django.urls import path
urlpatterns = [
path('form.html', views.render_form_View, name='form1'),
path('hl7', views.hl7_web_view ,name='hl7'),
]
Your paths don' t match the request.
You can create a TemplateView subclass for render your template:
Views
from django.views.generic.base import TemplateView
class HomePageView(TemplateView):
template_name = "display_form.html"
Url patterns
urlpatterns = [
path('', HomePageView.as_view(), name='home')
]

I am new at Django and kept being stuck at the ERROR 404 (Page Not Found)

Using the URLconf defined in firstProject.urls, Django tried these URL patterns, in this order: index/ [name='main-view'] admin/ The empty path didn’t match any of these.
from django.contrib import admin
from django.urls import include,path
from firstApp import views
urlpatterns = [
path('index/',views.index, name = 'main-view'),
path('admin/', admin.site.urls),
]
You don't have a url pattern for the empty route, hence, you are getting ERROR 404.
Change
path('index/',views.index, name = 'main-view'),
to
path('',views.index, name = 'main-view'),
in your urlpatterns list and your index view defined in your views.py will run.

Django flatpages The empty path didn't match any of these

On the administration page, I set the URL for the Django flat pages to "/", which is expected to be displayed as the home page at http://127.0.0.1:8000/. Doing so I encountered an error:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in core.urls, Django tried these URL patterns, in this order:
admin/
<path:url>
The empty path didn't match any of these.
But if I go to http://127.0.0.1:8000// with double slash then the home page is displayed correctly. My only urls.py file looks like this:
from django.contrib import admin
from django.urls import include, path
from django.contrib.flatpages import views
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns += [
path('<path:url>', views.flatpage),
]
And I took all the code completely from the official guide. How to display the django flatpage homepage at http://127.0.0.1:8000/?
In addition to the django.urls.path method Django offers the django.urls.re_path method. The path method is designed to perform matches against exact strings, whereas the re_path method is designed to perform matches against patterned strings based on regular expressions. In my case it’s enough to fix it this way:
urlpatterns += [
re_path(r'^(?P<url>.*)$', views.flatpage),
]
As a result, we get the correct processing of requests at http://127.0.0.1:8000/. More details about using path, re_path methods can be found at the link.

Python - Django add multiple urlpatterns for multiple views of template

I'm very very new to Python 3 and Django and I get to the following problem: I use a standard Template and now how to set it up when there is 1 view. But I don't get the code right for multiple views. I currently run the page locally
At the moment I have tried to change different orders within urlpatterns, and they do work when only 1 url in in there, but I can't get the second one in
views.py
from django.shortcuts import render, render_to_response
# Create your views here.
def index(request):
return render_to_response('index.html')
def store(request):
return render_to_response('store.html')
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from myapp import views as views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^store/$', views.store, name='store'),
url(r'^admin/', admin.site.urls)
]
urlpatterns += staticfiles_urlpatterns()
I would like the url pattern that lets me go to the index view and the store view
EDIT:
Full code is shared via: https://github.com/lotwij/DjangoTemplate
The error in the comments shows you are going to http:/127.0.0.1:8000/store.html, but your URL pattern url(r'^store/$', ...) does not include the .html, so you should go to http:/127.0.0.1:8000/store/.
The Django URL system uncouples the URL from the name of the template (sometimes the view doesn't even render a template!). You could change the regex to r'^store.html$ if you really want .html in the URL, but I find the URL without the extension is cleaner.

django urls: "Django tried these URL patterns"

I am trying a tutorial on Django called blog. I have the following structure:
FirstBlog|FirstBlog
settings
urls
__init__
etc
blog
templates | index.html
migrations
views.py
manage.py
The view.py has
from django.shortcuts import render
from django.shortcuts import render_to_response
from blog.models import posts
def home(request):
return render('index.html')
The urls.py has
from django.conf.urls import url
from django.conf.urls import include
from django.contrib import admin
urlpatterns = [
url(r'^blog', 'FirstBlog.blog.views.home',name='home'),
]
and I get this error:
Using the URLconf defined in FirstBlog.urls, Django tried these URL patterns, in this order: ^blog [name='home']
The current URL, , didn't match any of these.
I can't seem to get it right..
Any help would be appreciated.
Thank you,
You are requesting for / url and you have not saved any such mapping. Current mapping is for /blog . So it will work for the same url.
i.e goto the browser and request /blog
If you need it to work for / then change the urls appropriately.
within your blog app, create a urls.py file and add the following code which calls the home view.
blog/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
#url(r'^',views.home, name='home'),
]
then in your root urls file which can be found at FirstBlog/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^blog/',include('blog.urls')), #when you visit this url, it will go into the blog application and visit the internal urls in the app
]
PS:
your templates should be in blog/templates/blog/index.html
Read this docs on templates to understand how django locates templates.
This one is to understand how urls work Urls dispatcher
You are doing this in the wrong way! Try doing that using TemplateView from class-based views, which are the current standard from django views.
Check the django documentation: https://docs.djangoproject.com/en/1.9/topics/class-based-views/
Use this at the urls.py:
from django.conf.urls import url
from django.views.generic import TemplateView
urlpatterns = [
url(r'^blog/', TemplateView.as_view(template_name="index.html")),
]
And then, create a folder called templates in the project root ( like this: https://docs.djangoproject.com/en/1.9/intro/tutorial03/#write-views-that-actually-do-something ) named index.html
Simply go to file then select Save all your project instead of save. Or use shortcut Ctrl +k s on windows. Project should be able to sync and display the code on Django interface