I can't get my Django URLs to work - django

My code is as follows:
root urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('app.urls')),
]
My applications url is like so
urlpatterns = [
url(r'^$',views.login, name='login'),
url(r'^homepage/$', views.homepage, name='homepage'),
]
The first screen the user will see is the login screen (views.login). At the moment I just want to set the login button to be a url that takes them to the homepage (just for practice) but it doesnt seem to work.
The login html is like so
<button type="button">Log-In</button>
This should go to my urls page above...find the name 'homepage' and take me to views.homepage which is as so:
def homepage(request):
return render(request, 'application/homepage.html', {})
but my homepage doesnt get rendered and I have absolutely no idea why its driving me crazy.
Any help would be appreciated.

This is nothing to do with Django, but a pure HTML problem. You can't put a link inside a button. A button needs to be part of a form, and submits to the action value of that form. Either do that, and take the a tag out; or, remove the button, and just use the a.

Related

Change URL for wagtail blog index page

I currently have a website where the home page at www.mysite.com is the wagtail blog index page
I wish to move the blogindex page to another url
I can easily have a different homepage by amending my urls.py file:
#original
path("", include(wagtail_urls))
#new
path(
"",
TemplateView.as_view(template_name="pages/newhomepage.html"),
name="newhomepage",
),
However I would like the blogindex page available at e.g. myste.com/blog but I am not sure how to go about this. Adding the following to urls.py does not do it
path("blog/", include(wagtail_urls))
There are a couple of changes in project required to achieve this.
Firstly, create a urls.py file inside your specific app, this will be different from the one that you should already have alongside wsgi.py, asgi.py, etc. and it will only store app specific urls.
from django.urls import path
from .views import index
urlpatterns = [
path('test/', index, name='generalized-test-url'),
]
Now, you should render the template inside your view.
def index(request):
return render(request, 'pages/newhomepage.html', context)
Lastly, in your root urls.py file, you need to include the app urls. Lets say your app name is blog.
from django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls')),
]

Django breaks on urls containing "logout"

I'm working on a Django web-app which has user accounts, and so has login and logout functions.
What I would like to have is mywebsite.com/accounts/login/ be the login page. This is working as expected right now.
This issue is with logout- what I would like to have is mywebsite.com/accounts/logout/ logout the user and redirect them to the login page. This, however, doesn't appear to work.
Here is my url configuration in accounts/urls.py:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logout/$', auth_views.logout, name='logout'),
]
With this configuration, login works fine. But when I go to mywebsite.com/accounts/logout/, I am just immediately sent back to the page I'm currently on.
However, if I change the logout url:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logMeOutPlease/$', auth_views.logout, name='logout'),
]
Then mywebsite.com/accounts/login works as intended and mywebsite.com/accounts/logMeOutPlease works as intended. Is there a reason the first configuration won't work?
If I however move the logout functionality to the top level (i.e just mywebsite.com/logout/), then it again works fine.
For reference, this is what is in the "top level" urls file:
urlpatterns = [
# other urls that I can't show here
url(r'^redirect/', include('mywebsite.apps.redirect.urls')),
url(r'^accounts/', include('mywebsite.apps.accounts.urls')),
url(r'^$', RedirectView.as_view(url='redirect/')),
url(r'^admin/', admin.site.urls),
]
I am using Django 2.0.6 and Python 3.5.2, and my laptop is on Ubuntu 16.04.
Ok, so from what I understand so far, that behavior is actually correct. Since, views logic was not provided in your question, I will assume that the views are currently not using any #login_required decorators and the logout view does what it says i.e. logout. So without seeing the views logic we assume there were no instructions about where to redirect to on successful logout and no decorator so opens the same page again.
So here's the advice.
In your settings.py file:-
Add:
LOGOUT_REDIRECT_URL = 'url_you_want_to_redirect_to_on_logout'
This will enable redirecting to the specified url whenever a user logs out.
And if it helps, similarly we also do have a
LOGIN_REDIRECT_URL = 'url_you_want_to_redirect_to_on_login'

How to extend django.contrib.auth views?

How I've Arranged Templates : I have placed my login.html in /templates/registration folder of Django. So, the Django takes necessary care of accounts/login ,accounts/logout url requests and renders as per request. And I haven't to code for the individual login and logout functions.
What I am trying to Achieve : I want to authenticate users at login request, when they requests the login page:
If user is anonymous user, I want to render the normal login page.
However, If the user is authenticated thats already logged in. I want to display an error and not the logged page.
I want to achieve this in the views.py and urls.py and not in the templates by:
{% if user.is_authenticated %}
{% if user.is_anonymous %}
Urls.py
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
urlpatterns=[
url(r'^register/$', views.register, name='register'),
url(r'^logout/$', auth_views.logout, {'next_page' : 'Homepage'}, name='logout'),
]
You can wrap the view with your own, which either redirects or calls the original.
def wrapped_login(request):
if request.user.is_authenticated:
return redirect('whatever')
else:
return auth_views.login(request)
Provide LOGIN_REDIRECT_URL = '/' in settings.py. Then use the following url for login page:
urlpatterns = [
url(r'^login/',
auth_views.LoginView.as_view(redirect_authenticated_user=True),
name='login'),
]
This will redirect your user to the URL provided in settings file if they try to login even after being authenticated.

Django URL regex with variables

Was hoping someone could point me in the right direction with this. I've tried nearly everything I can think of, but I can't seem to get it to work. I've got a set of URLs I'd like to match in Django:
www.something.com/django/tabs/
www.something.com/django/tabs/?id=1
Basically, I want to make it so that when you just visit www.something.com/django/tabs/ it takes you to a splash page where you can browse through stuff. When you visit the second URL however, it takes you to a specific page which you can browse to from the first URL. This page is rendered based on an object in the database, which is why the id number is there. I've tried to account for this in the URL regex, but nothing I try seems to work. They all just take me to the main page.
This is what I have in urls.py within the main site folder:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tabs/', include("tabs.urls")),
]
and within urls.py in the app's folder:
urlpatterns = [
url(r'\?id=\d+$', tab),
url(r'^$', alltabs)
]
Would anyone be so kind as to point me in the right direction? Thanks in advance!
You are not following the right approach here. Query paramers are used to change the behaviour of the page slightly. Like a added filter, search query etc.
What i would suggest is you have only one view and render different templates based on query parameters in the view.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tabs/', alltabs),
]
In your alltab views you can have something like this.
def alltabs(request):
if request.GET.get("id"):
id = request.GET.get("id")
your_object = MyModel.objects.get(id=id)
return render_to_response("tab.html", {"object":your_object})
return render_to_response("alltab.html")
Hope this helps
This is not the preferred 'django way' of defining urls patterns, I would say:-)
In the spirit of django would be something like
www.something.com/django/tabs/
www.something.com/django/tabs/1/
....
www.something.com/django/tabs/4/
and for this you define your url patterns within the app for example this way
tabs/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /tabs/
url(r'^$', views.index, name='index'),
# ex: /tabs/5/
url(r'^(?P<tab_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /tabs/5/results/
url(r'^(?P<tab_id>[0-9]+)/results/$', views.results, name='results'),
]
and something similar in your views
tabs/views.py:
from django.shortcuts import get_object_or_404, render
from tabs.models import Tab
def index(request):
return render(request, 'tabs/index.html')
def detail(request, tab_id):
tab = get_object_or_404(Tab, pk=tab_id)
return render(request, 'tabs/detail.html', {'tab': tab})
...
You can follow this django tutorial for more details:

Django URL changes but doesn't render the proper view

I have a url setup with the following view (the url is in the app and the app urls are included in the project):
url(r'^details/(?P<outage_id>\d+)/$', 'outage.views.outage_details'),
def outage_details(request, outage_id=1):
outage_info = Outages.objects.filter(id=outage_id)
return render_to_response('templates/outage/details.html', {'outage_info': outage_info}, context_instance=RequestContext(request))
When I click on the link from http://localhost:8000 the url in the browser changes to http://localhost:8000/outage/details/1 as it should, but the view doesn't render the right template. The page stays the same. I don't get any errors, the url changes in the browser but the details.html template doesn't render. There is an outage in the DB with an ID of 1.
Any thoughts?
The regular expression r'^details/(?P<outage_id>\d+)/$' does not match the URL http://localhost:8000/outage/details/1. However, it should match the expression r'^outage/details/(?P<outage_id>\d+)/$'.
Perhaps, you can post your entire urls.py to find out which view is actually being called, since you don't get any errors. I suspect your home page is being called for all URLs.
Here is my url setup:
project/urls.py
urlpatterns = patterns('',
url(r'^$', 'outage.views.show_outages'),
url(r'^inventory/', include('inventory.urls')),
url(r'^outage/', include('outage.urls')),
url(r'^login', 'django.contrib.auth.views.login', {'template_name': 'templates/auth/login.html'}),
url(r'^logout', 'django.contrib.auth.views.logout', {'next_page': '/'}),
url(r'^admin/', include(admin.site.urls)),
)
outage/urls.py
urlpatterns = patterns('',
url(r'^', 'outage.views.show_outages'),
url(r'^notes/(?P<outage_id>\d+)/$', 'outage.views.outage_notes', name='notes'),
)
I have since changed the details to notes, since I had another page in a different app with a details url and I didn't want it somehow confusing things.