Can't render template, getting page not found (404) Django - python

I can't seem to find the error, site works fine but when I request http://127.0.0.1:8000/test I get the 404 error, I feel so stupid I'm really stuck on this, please help:(
Error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/test
Using the URLconf defined in project4.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
login [name='login']
logout [name='logout']
register [name='register']
posts-json/ [name='posts-view-json']
^network/static/(?P<path>.*)$
^media/(?P<path>.*)$
The current path, test, 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.
This is my urls
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
from .views import post_view_json, profile_test_view
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("test/", profile_test_view, name="test"),
# endpoints
path("posts-json/", post_view_json, name="posts-view-json")
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
It's a social website and I'm trying to render the posts and user's profiles on a page.
My views
from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.http.response import JsonResponse
from django.shortcuts import render
from django.urls import reverse
from django.core import serializers
from .models import User, Post, Profile
def index(request):
qs = Post.objects.all()
context = {
'hello': 'hello world!',
'qs':qs,
}
return render(request, "network/index.html", context)
def post_view_json(request):
qs = Post.objects.all()
data = serializers.serialize('json', qs)
return JsonResponse({'data': data})
def profile_test_view(request):
profile = Profile.objects.get(user=request.user)
return render(request, "network/profile_test.html", {'profile':profile})

You have a trailing slash in urls.py but not in the URL you are calling.
Either change urls.py like this:
...
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("test", profile_test_view, name="test"), # <-- NOTE: I removed the slash here
# endpoints
path("posts-json/", post_view_json, name="posts-view-json")
]
...
or call http://127.0.0.1:8000/test/ <-- NOTE: slash added

Related

Django logout not working - can't understand why

Hi guys hope someone can help me here. I am just starting to create a simple web app using django and I am confused why this isn't working.
views.py
from django.shortcuts import render, redirect
from django.contrib.auth import login, logout
def index(request):
return render(request, "fittracker/main.html")
def login_view(request):
pass
def logout_view(request):
logout(request)
return redirect("fittracker/main.html")
def signup(request):
pass
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path("logout/", views.logout, name='logout')
]
I am getting this error
I have tired looking at the official docs, and this should redirect, but I am not sure why it isn't
The name of the view is logout_view, so it hould be views.logout_view:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('logout/', views.logout_view, name='logout')
]
Now you use the logout that you re-exported from the django.contrib.auth module.

Using the URLconf defined,Django tried these URL patterns

I'm doing tutorial "Movie rental" and i got error.
Using the URLconf defined in vidly.urls, Django tried these URL patterns, in this order:
Using the URLconf defined , Django tried these URL patterns, in this order:
1.admin/
2.movies/ [name='movie_index']
3.movies/ <int:movie_id [name='movie_detail']
The current path, movies/1, didn't match any of these.
my code is(from main urls.py):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('movies/', include('movies.urls'))
]
from mysite i.e movies(urls.py)
from . import views
from django.urls import path
urlpatterns = [
path('', views.index, name='movie_index'),
path('<int:movie_id', views.detail, name='movie_detail')
]
from views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Movie
def index(request):
movies = Movie.objects.all()
return render(request, 'movies/index.html', {'movies': movies})
def detail(request, movie_id):
return HttpResponse(movie_id)
What i'm doing wrong?
In your movies.urls you forgot to close URL parameter.
path('<int:movie_id>/', views.detail, name='movie_detail')
Instead you did,
path('<int:movie_id', views.detail, name='movie_detail')
You're missing a closing >/.
path('<int:movie_id>/', views.detail, name='movie_detail')

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/catalog/

I got the follwoing error whenever I run the project and automatically catalog/ is added to the url:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/catalog/
Using the URLconf defined in first_project.urls, Django tried these URL patterns, in this order:
[name='index']
admin/
first_app/
The current path, catalog/, 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.
Here is my project urls.py
from django.contrib import admin
from django.urls import path, include
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('first_app/', include('first_app.urls')),
]
Here is first_app urls.py code:
from django.urls import path
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
]
How to get the index page as a default and get rid of the catalog.
Here is views.py file:
from django.shortcuts import render
from django.http import HttpResponse
from first_app.models import Topic, AccessRecord, Webpage
# Create your views here.
def index(request):
webpages_list = AccessRecord.objects.order_by('date')
date_dict = {'access_records': webpages_list}
return render(request, 'first_app/index.html', context=date_dict)
Hey Mak in your code u didnt mention any url related to /catalog/ so you are getting that 404 error message. Error 404 means that the page couldnt found by the server add /catalog/ in your url patterns so that the server could know which page is to be shown if /catalog/ is requested . And as far as ur proble"and automatically catalog/ is added to the url:" may be its your browser autocompleting the address field so once plesase check it. if the request is only /catalog then ur myproject urls.py file should be something like this
from django.contrib import admin
from django.urls import path, include
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('first_app/', include('first_app.urls')),
path('catalog',views.your_function_name)
]
if the request is first_app/catalog then ur first_app urls.py file should be something like this in url patterns
urlpatterns = [
path('/catalog', views.catalog, name='catalog'),
]
from django.contrib import admin
from django.urls import path, include
from first_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('first_app/', include('first_app.urls')),]
and
from django.urls import path
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
]
>>>>> if your templates files :
templates/index.html
def index(request):
webpages_list = AccessRecord.objects.order_by('date')
date_dict = {'access_records': webpages_list}
return render(request, 'index.html', context=date_dict)

Page not found (404) Request Method: GET

I keep getting this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/home
Using the URLconf defined in personal_portfolio.urls, Django tried these URL patterns, in this order:
admin/
home/
The current path, home, didn't match any of these.
This is my urls.py for the overall project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include('hello_world.urls')),
]
this is my code for hello_world urls.py
from django.urls import path
from hello_world import views
urlpatterns = [
path('home/', views.hello_world, name='hello_world'),
]
this is my code for hello_world views.py
from django.shortcuts import render
def hello_world(request):
return render(request, 'hello_world.html')
you need to change this url from
urlpatterns = [
path('home/', views.hello_world, name='hello_world'),
]
to
urlpatterns = [
path('', views.hello_world, name='hello_world'),
]
to get to http://127.0.0.1:8000/home/
else you need to goto http://127.0.0.1:8000/home/home/ for the current url

How to configure Django views and urls to render specific templates

When I bring up 127.0.0.1:8000, the current page that show up is something.html template.
I would need to make it appear index.html at first launch, then when I click on other parts,it should go to 127.0.0.1:8000/something.html (or 127.0.0.1:8000/myapp/something.html).
What would be the structure to achieve this?
I frequently get error message : The current URL didn't match any of these.
Currently, my structure is
project
---myapp
---admin.py
---models.py
---url.py
---views.py
---static
---templates
---myapp
---html files
---mysite
---settings.py
--- url.py
under my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
under mysettings/url.py
urlpatterns = [
# Examples:
url(r'^$', 'mysite.views.home', name='home'),
url(r'^myapp/', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = [
# Examples:
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
admin.site.site_header = 'Admin'
myapp/url.py
from . import views
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
#brings up something.html when we comment it out. No module found if included.
#url(r'^$', views.home, name='home'),
url(r'^$', views.post_list, name='post_list'),
)
Include urls of your myapp app in mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
Now, all urls, starting by 127.0.0.1:8000, will check if there is a view that can handle the request. I recommend you to read this to understand how Django URL dispatcher works : [Django documentation - URL Dispatcher] (https://docs.djangoproject.com/en/1.8/topics/http/urls/).
2. Add new route in your myapp.urls:
from django.conf.urls import url, patterns
from . import views
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^something$', views.something, name='something'),
url(r'^posts$', views.post_list, name='post_list'),
)
Now :
127.0.0.1:8000/ will executed code of views.home
127.0.0.1:8000/something will executed code of views.something
127.0.0.1:8000/posts will executed code of views.post_list
Let's define these view now
3: Define the views in myapp.views :
from django.shortcuts import render
def home(request):
"""
Return home page
"""
return render(request, 'myapp/home.html')
def something(request):
"""
Return something page
"""
return render(request, 'myapp/something.html')
def post_list(request):
"""
Return something page
"""
# do what you want
Add your templates in myapp/templates/myapp/. Add home.html and something.html.
Now forget, the `.html
You create a url (with attached view to it).
In the view you render any html page you want.
If you use function based views your 'mysite.views.home' may look like:
def home(request):
...
return render(request, 'path/to/index.html')
and so on.
It's the basics so I won't talk about this much. You can find a good tutorial about mapping urls to views there.

Categories

Resources