Django views in project directory - django

I have a project with two apps with their respective views, but I want to create a views.py in django project directory for some generic pages such as about, login... It is correct to place the views.py and templates in the root project folder for this purpose?

I have been reading about this. The practice more recommended for this is create an app that creates cohesion between other apps, call it web_site or site whatever is better for you.
python manage.py startapp my_site
Everything in this site app will be done the regular way.
In the projects url you will want to import the url in this way so the web pages appear in the / url pattern.
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_site.urls'))
]

I do this for project level pages like you describe. These are usually simple pages that tie the project's apps together, and not contain any business logic.

You can do that by making views.py file into the project directory.
from django.contrib import admin
from django.urls import path, include
from attendance import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('insert.urls')),
path('login/', views.logins, name='login'),# relevant line
]
And this is the code of login view
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def logins(request):
return HttpResponse("THIS IS LOGIN PAGE !!!")

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')),
]

how to correctly import urls.py from app?

This is probably pretty simple but I can't get my head around it. I'm learning Django, have v3.0.4 installed and can't get the URLs from an app to work correctly.
On the project urls.py I have the following:
Project\urls.py:
from django.contrib import admin
from django.urls import path
from django.urls import include
from AppTwo import views
urlpatterns = [
path('', views.index, name='index'),
path('', include('AppTwo.urls')),
path('admin/', admin.site.urls),
]
I've created an app named "AppTwo" and have the following urls.py and views.py in the app:
AppTwo\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('/help', views.help, name='help'),
]
AppTwo\views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<em>My Second App</em>")
def help(request):
return HttpResponse("<em>Help Page!!!</em>")
If I browse to http://127.0.0.1:8000/ the index page loads and I see the text "My Second App" as expected. However if I browse to http://127.0.0.1:8000/help I get page not found 404 error.
I can also browse to the admin page just fine. So far this is a stock project, the only other change I made after creating it was to the settings.py file to install the "AppTwo" application. Based on the documentation, this looks like it should work, so what am I doing wrong?
yep, knew it was simple.
Changed
path('/help', views.help, name='help'),
to:
path('help/', views.help, name='help'),
all good now.

Django URLs management

I'm a full beginner on Django. Therefore, I'm sorry if my question is not making so much sense.
I'm studying Django tutorial - step 1. I installed properly Django and created my first Django project named 'vanilla'. The urls.py script of the vanilla project is
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
When I start Django with this 'vanilla' project and start the development server at http://127.0.0.1:8000/, I correctly see the start page (with a rocket).
In a second project that I named 'djtutorial', I created as requested in Django tutorial - step 1 a 'polls' app. As requested, I modified urls.py file in djtutorial\polls which now has following content:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
If I start the server the url http://127.0.0.1:8000/polls/ is working properly. However, if I launch the root url of the site (i.e. http://127.0.0.1:8000/), I now have a 404 error.
Why is the 'root url' of the site now hidden?
There wasn't any root URL at all. Main page informing you that your project is created properly shows up only if you don't have any view of your own added to urlpatterns. When you create any page, this welcome page stops showing up and that is expected.
If you want to show your own page on root of your website, use this in your urlpatterns:
path('', >>VIEW OR INCLUDE HERE<<),

In Django 1.11 getting Circular Import Errors When including app URLconf files in project URLconf File

I'm new to Django, have been doing several tutorials to get really comfortable with the structuring, and am now running through the official tutorial.
I've created an polls App, which has a polls/views.py file as follows:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, World. You're at the polls index.")
I've also created an App URLconf file polls/urls.py with the following code:
from django.conf.urls import url
from . import views
url_patterns = [
url(r'^$', views.index, name='index'),
]
This is pretty much exactly as done in the Django tutorial.
My issue is when I'm specifying url routes in the main projectname/url.py file on a project level as such:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
When doing this, I get the following error:
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'polls.urls' from 'ProjectFolder\\polls\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
This is how the official Django tutorial dictates it to be done. However, if I explicity import the polls/views.py file from the app, I can accomplish the task as follows:
from django.conf.urls import url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', views.index),
]
My immediate concern is the import of every app/urls file I ever create being necessitated by this approach, as well as the obvious divergence from the official Django instruction.
I hesitated to even ask this question because I feel that such a fundamental issue has bound to have an easy fix. Any help would be greatly appreciated.
To clarify, I can get around the error by explicitly importing the view files from apps. Whenever using the Django documentation-described approach of using the include() function I receive the error. I can appreciate the value of this function, and would like to know why is giving me the error described above.
Just writte urlpatterns = [ .. and not url_patterns in your poll.views.py.

django urls: "Django tried these URL patterns"

I am trying a tutorial on Django called blog. I have the following structure:
FirstBlog|FirstBlog
settings
urls
__init__
etc
blog
templates | index.html
migrations
views.py
manage.py
The view.py has
from django.shortcuts import render
from django.shortcuts import render_to_response
from blog.models import posts
def home(request):
return render('index.html')
The urls.py has
from django.conf.urls import url
from django.conf.urls import include
from django.contrib import admin
urlpatterns = [
url(r'^blog', 'FirstBlog.blog.views.home',name='home'),
]
and I get this error:
Using the URLconf defined in FirstBlog.urls, Django tried these URL patterns, in this order: ^blog [name='home']
The current URL, , didn't match any of these.
I can't seem to get it right..
Any help would be appreciated.
Thank you,
You are requesting for / url and you have not saved any such mapping. Current mapping is for /blog . So it will work for the same url.
i.e goto the browser and request /blog
If you need it to work for / then change the urls appropriately.
within your blog app, create a urls.py file and add the following code which calls the home view.
blog/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
#url(r'^',views.home, name='home'),
]
then in your root urls file which can be found at FirstBlog/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^blog/',include('blog.urls')), #when you visit this url, it will go into the blog application and visit the internal urls in the app
]
PS:
your templates should be in blog/templates/blog/index.html
Read this docs on templates to understand how django locates templates.
This one is to understand how urls work Urls dispatcher
You are doing this in the wrong way! Try doing that using TemplateView from class-based views, which are the current standard from django views.
Check the django documentation: https://docs.djangoproject.com/en/1.9/topics/class-based-views/
Use this at the urls.py:
from django.conf.urls import url
from django.views.generic import TemplateView
urlpatterns = [
url(r'^blog/', TemplateView.as_view(template_name="index.html")),
]
And then, create a folder called templates in the project root ( like this: https://docs.djangoproject.com/en/1.9/intro/tutorial03/#write-views-that-actually-do-something ) named index.html
Simply go to file then select Save all your project instead of save. Or use shortcut Ctrl +k s on windows. Project should be able to sync and display the code on Django interface