how can I get the URL path for App
def logout(request):
auth.logout(request)
# path defined for the app in the projects urls.py
return HttpResponseRedirect('??????')
I know I can hard code it but how can I get it?
urls.py file
urlpatterns = patterns('',
url(r'^chat/',include('djangoChat.urls')),
djangoChat.urls.py file
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^login/$',views.login,name='login'),
url(r'^logout/$',views.logout,name='logout')
)
i want logout method to redirect to /chat path
so views.index method gets called
To reverse lookup a URL by name, do:
from django.core.urlresolvers import reverse
def logout(request):
auth.logout(request)
return HttpResponseRedirect(reverse('index'))
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 am trying to show a blogpost on the site.
Below is details of urls and view file details but still it showing a 404 not found page
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="ShopHome"),
path("blogpost/", views.blogpost, name="blogpost")
]
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request, 'blog/index.html')
def blogpost(request):
return render(request, 'blog/blogpost.html')
Showing:
404 not found page
you should include urls.py of your application to project urls.py
actually if your project name is "mysite" and your application name is "blogspot", you should include mysite/blogspot/urls.py on this file mysite/mysite/urls.py like this:
from django.urls import path, include
urlpatterns = [
path('', include('blogspot.urls')),
]
Actually, i forget to add the slash at the time of registering the url of this app in urls
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.
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.