How does django know where to load login.html - django

I have used the following steps to create a login page.
1> create the urls.py
urlpatterns = patterns('',
(r'^$', main_page),
(r'^login/$', 'django.contrib.auth.views.login'),
)
2> create registration/login.html
3> load http://127.0.0.1:8000/login/ then I see the created login.html.
Also my directory structure is as follows:
.
├── bookmarks
│   ├── forms.py
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── bookmarksdb
├── __init__.py
├── manage.py
├── settings.py
├── site_media
│   └── style.css
├── templates
│   ├── base.html
│   ├── main_page.html
│   ├── registration
│   │   ├── login.html
│   │   ├── logout_success.html
│   │   ├── register.html
│   │   └── register_success.html
│   ├── user_page.html
└── urls.py
Question> How does django know to connect the http://127.0.0.1:8000/login/ with template\registration\login.html?
Thank you

if you look in django.contrib.auth.views.py you'll find
def login(request, template_name='registration/login.html'),
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm,
current_app=None, extra_context=None):
"""
Displays the login form and handles the login action.
"""
it's simply the default template path, which you can override should you wish

Related

Why cannot i access the about page when i can access the home page in the same directory?

I can access the home.html (http://127.0.0.1:8000/) without any issues but about page (http://127.0.0.1:8000/about/) gives me the below error:
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
admin/
[name='blog-home']
about [name='blog-about']
The current path, about/, didn't match any of these.
django project folder urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
app folder urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
path('about', views.about, name='blog-about'),
]
app folder views.py:
from django.shortcuts import render
def home(request):
return render(request, 'blog/home.html')
def about(request):
return render(request, 'blog/about.html')
directory structure:
.
├── blog
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── apps.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── static
│   │   └── blog
│   │   ├── css
│   │   │   ├── main.css
│   │   │   └── test.css
│   │   ├── images
│   │   │   ├── favicon.png
│   │   │   └── freedom.jpg
│   │   └── js
│   ├── templates
│   │   └── blog
│   │   ├── about.html
│   │   ├── home.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── django_project
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── wsgi.cpython-37.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
12 directories, 36 files
u have to add a trailing slash to ur path urls in urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
path('about/', views.about, name='blog-about'),
]

Why is my store.html not recognized?

I am Learning Django Udemy course(James Carrigan),I only updated to 2.0.5
tree bookstore
bookstore
├── bookstore
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
└── store
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│   ├── __init__.py
│   └── __pycache__
│   └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│   ├── admin.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   ├── models.cpython-36.pyc
│   └── views.cpython-36.pyc
├── templates
│   ├── store.html
│   └── template.html
├── tests.py
└── views.py
These are my views
def index(request):
return render(request,'template.html')
def store(request):
return render(request,'store.html')
Urls
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from store import views
urlpatterns = [
url(r'^', views.index ,name='index' ),
url(r'^store', views.store ,name='store' ),
path('admin/', admin.site.urls),
]
I have changed line in HTML Boilerplate file
<p>Welcome to mystery books and store template is changed!</p>
I have problem when I go runserver
It does not see any difference between
http://127.0.0.1:8000/store and
http://127.0.0.1:8000/index
Why is my other html not recognized?
You don't end your regex pattern, so the first one matches everything.
Make sure you end your regex expression with $
url(r'^$', views.index ,name='index' ),
url(r'^store$', views.store ,name='store' ),

Redirect url from django application

I have a basic application built in Django, which only works if I enter:
http://xx.xx.xxx.xx/polls. How can I rewrite this in my urls.py file so that http://xx.xx.xxx.xx/polls will redirect me to http://xx.xx.xxx.xx/ ?
My urls.py file for the main project:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
My urls.py file from the application:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
]
My project structure:
├── blog
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── db.sqlite3
├── manage.py
├── polls
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   └── polls
│   │   ├── detail.html
│   │   ├── index.html
│   │   ├── results.html
│   │   └── static
│   │   └── polls
│   │   ├── css
│   │   │   ├── semantic.css
│   │   │   ├── semantic.min.css
│   │   │   ├── sidebar.css
│   │   │   ├── sidebar.min.css
│   │   │   ├── sidebar.min.js
│   │   │   └── style.css
│   │   ├── images
│   │   └── js
│   │   ├── jquery-1.11.2.min.js
│   │   ├── semantic.js
│   │   ├── semantic.min.js
│   │   ├── sidebar.js
│   │   └── sidebar.min.js
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── readme.txt
├── requirements.txt
├── templates
│   └── admin
│   └── base_site.html
As far as I undesrstand you don't ant to redirect from /polls/ to / but want to show the polls index at the home page. If so then just move the index url from the polls/urls.py into the main urls.py:
from polls.views import IndexView
urlpatterns = [
url(r'^$', IndexView.as_view(), name='index'),
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
UPDATE: It is a bad practice to use hard-coded urls in the django templates/code. You should always use the {% url %} template tag and the reverse() function. This will allow you to change urls as you want without breaking the code.
So link to the index page should be like this:
Home page
And, for example, link to the poll details:
Poll #{{ poll.pk }}
In the model's get_absolute_url() method use the reverse().
You could also include your whole polls app on '/' by doing:
url(r'^', include('polls.urls', namespace="polls")),
Also, see here for more information on redirecting in urls.py:
Redirect to named url pattern directly from urls.py in django?
In your case it would be something like:
from django.views.generic import RedirectView
url(r'^polls/', RedirectView.as_view(pattern_name='polls:index')

Django - how to wire urls correctly? url(), include(), ImportError

I try to structure my project by putting applications under an "apps" folder, like so:
├── manage.py
├── mysite
│   ├── apps
│   │   ├── __init__.py
│   │   ├── myapp1
│   │   │   ├── __init__.py
│   │   │   ├── models.py
│   │   │   ├── urls.py
│   │   │   └── views.py
│   │   └── myapp2
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
And in mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^myapp1/', include('mysite.apps.myapp1.urls')),
url(r'^mysite/apps/myapp2/', include('myapp2.urls')),
url(r'^admin/', include(admin.site.urls)),
)
There is something wrong with:
url(r'^myapp1/', include('mysite.apps.myapp1.urls')),
url(r'^mysite/apps/myapp2/', include('myapp2.urls')),
I could not wire either myapp1 or myapp2 correctly, Django gives me "ImportError...no module named myapp1..." Any help?
You're missing a level in the relative path:
url(r'^mysite/apps/myapp2/', include('apps.myapp2.urls')),
myapp1 looks like it should work to me.
A note, comparing how you're trying to include myapp1 vs myapp2, it looks like you may have misunderstood the structure slightly. The URL has nothing to do with the code layout. This is completely valid:
url(r'^zimzam/allthethings/', include('apps.myapp2.urls')),
maybe like this:
include('mysite.apps.myapp1.urls')),
update
you can try:
add a file __init__.py in the mysite dir

Override django userena views

I know there is a plethora of questions related to this, like this one, but I does not seem to help.
My question is simple, given this function signature, how do I override the view profile_detail? I seem to be doing everything exactly as both resources say, but my templates are still not receiving the extra context. Here's what I am trying to do:
# This overrides and extends userena's view named 'profile_detail'.
def profileDetailView(request, username):
# Do some logic, etc.
foo_list = Some.objects.filter(id=username)
extra_context = {'foo_list': foo_list}
# I have also tried the following for extra_context:
extra_context['foo_list'] = foo_list
return userena_views.profile_detail(request, username, template_name='userena/profile_detail.html',
extra_context=extra_context)
I am not sure where I am going wrong. I have my URL's correct, as it is loading my views without error. Also, I have tested through the shell to create foo_list by import objects - it worked. I have also done a test to make sure the username being passed in actually results in an object being found. Thus - it seems like the only problem could be in the extra_context, at least I hope! Any help would be much appreciated!
EDIT: File structure Request
├── project
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── static
│   │   ├── css
│   │   │   └─
│   │   └── js
│   ├── test.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── manage.py
├── media
├── templates
│   ├── admin
│   │   └── base_site.html
│   ├── base_site.html
│   ├── errors
│   │   └── not_authorized.html
│   └── userena
│   ├── base.html
│   ├── base_userena.html
│   ├── profile_detail.html
│   ├── profile_details.html
│   └── signup_form.html
└── utils
TEMPLATE_DIRS = (
'/home/username/project/project/templates',
)