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.
Related
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
When def login() function call and redirect to def index() function my url change in the browser and look like http://127.0.0.1:8000/error500index this url. But logically url look like http://127.0.0.1:8000/index this. But error500 show in the url when i use redirect function, error500 is my last url in the Project urls.py and APP urls.py.
Anyone help me out what is the happening?
view.py
from django.shortcuts import render, redirect
from django.contrib import messages
from django.http import HttpResponse
from .models import WebUser
def index(request):
return render(request, 'index.html')
def login(request):
if (request.method == 'POST'):
login_email = request.POST['email']
login_password = request.POST['password']
# Compare with Database where input email exist!
try:
CheckUser = WebUser.objects.get(email=login_email)
except:
return HttpResponse("User Dosen't Exist!")
if (login_email == CheckUser.email and login_password == CheckUser.Password):
#When redirect function call my url change and pick the last url from project urls.py and this url appears in the browser http://127.0.0.1:8000/error500index
return redirect(index)
else:
return HttpResponse("Email or Password are wrong!")
else:
return render(request, 'login.html')
Project Urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('SIS_APP.urls')),
path('index', include('SIS_APP.urls')),
path('login', include('SIS_APP.urls')),
path('register', include('SIS_APP.urls')),
path('settings', include('SIS_APP.urls')),
path('weather', include('SIS_APP.urls')),
path('error404', include('SIS_APP.urls')),
path('error500', include('SIS_APP.urls')),
]
APP urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('index', views.index, name='index'),
path('login', views.login, name='login'),
path('register', views.register, name='register'),
path('settings', views.settings, name='settings'),
path('weather', weatherAPI.weather, name='weather'),
path('error404', views.error404, name='error404'),
path('error500', views.error500, name='error500'),
]
You are missing the slashes "/" in your urls. URLs are concatenated, so if you have, i.e,
path('index', views.index, name='index'), ## this will give ..indexsome-sub-route
path('index/', views.index, name='index'), ## this will give ..index/some-sub-route
i think you are not defining your urls correctly . from django docs django docs
you will see that redirect can be used with a hardcoded link that is
redirect(/index/)
and since you didnt add the slash in the url we have
redirect(index)
so it will pass the the value index to your url.
to fix it add / to ur url defination
I want to display a simple hello in the server but receiving an error as Page not found.
here are my code:
Analytic_web.urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path(r'jas/', include('website.urls')),
path('admin/', admin.site.urls),
]
website.urls.py:
from django.urls import path
from . import views
urlpatterns = [
path(r'jas/', views.home, name='home'),
]
views.py:
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, '/templates/website/home.html' )
home.html:
<h1>Helllllllloooooooooowwwwwwwwwwwww</h1>
in website.urls.py, try :
path('', views.home, name='home'),
instead of this
path(r'jas/', views.home, name='home'),
and in Analytic_web.urls.py, try :
path('jas/', include('website.urls')),
instead of this
path(r'jas/', include('website.urls')),
hope this would help.
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')
My Django project has 5 apps: app1, app2 ......
each app has one template index.html and 1 view named index.
project/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('main.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
app1/urls.py
from django.conf.urls import url, include
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^accounts/login/$', auth_views.login, name='login'),
app1/views.py
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required, user_passes_test
def is_member(user):
return user.groups.filter(name='group1').exists()
#login_required
#user_passes_test(is_member)
def index(request):
getting our template
template = loader.get_template('app1/index.html')
rendering the template in HttpResponse
return HttpResponse(template.render())
setting.py
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/'
The 5 apps requirer login based on group
app one I have no problem to login but the reset of the apps after login succeed, I will be redirected to login page again.
The reset of the apps has exactly the same code except the group name and template location.
Also the logger result show me that there is no problem with credential.
I tried to change the group nae for other app as app1 and still I am getting same result.
Please advice me how to solve this problem.