How to configure Django views and urls to render specific templates - python

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.

Related

django path to view

hey i have a url path that i want to lead me to an application url file
but it says page not found
here is my core url:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls', namespace='store')),
path('basket/', include('basket.urls', namespace='basket')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and here is my basket.url file:
from django.urls import path
from . import views
app_name = 'basket'
urlpatterns = [
path('', views.basket_summary, name='basket_summary'),
]
and this is the view:
from django.shortcuts import render
# Create your views here.
def basket_summary(request):
return render(request, 'store/basket/summary.html')
my app name is basket , and i might add all of the files are defined.
whats the problem ?
You should add a trailing slash after /basket/.
Read more about APPEND_SLASH in the official documentation.

i am trying to learn django from django documentation but i am getting an error message in the first part of the documentation

Using the URLconf defined in blogs.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
this is the error i am getting after i wrote the following code:
for urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
for polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
3.for polls/views.py
"""
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
"""
how i can i remove this error in vs code

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/catalog/

I got the follwoing error whenever I run the project and automatically catalog/ is added to the url:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/catalog/
Using the URLconf defined in first_project.urls, Django tried these URL patterns, in this order:
[name='index']
admin/
first_app/
The current path, catalog/, 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.
Here is my project urls.py
from django.contrib import admin
from django.urls import path, include
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('first_app/', include('first_app.urls')),
]
Here is first_app urls.py code:
from django.urls import path
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
]
How to get the index page as a default and get rid of the catalog.
Here is views.py file:
from django.shortcuts import render
from django.http import HttpResponse
from first_app.models import Topic, AccessRecord, Webpage
# Create your views here.
def index(request):
webpages_list = AccessRecord.objects.order_by('date')
date_dict = {'access_records': webpages_list}
return render(request, 'first_app/index.html', context=date_dict)
Hey Mak in your code u didnt mention any url related to /catalog/ so you are getting that 404 error message. Error 404 means that the page couldnt found by the server add /catalog/ in your url patterns so that the server could know which page is to be shown if /catalog/ is requested . And as far as ur proble"and automatically catalog/ is added to the url:" may be its your browser autocompleting the address field so once plesase check it. if the request is only /catalog then ur myproject urls.py file should be something like this
from django.contrib import admin
from django.urls import path, include
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('first_app/', include('first_app.urls')),
path('catalog',views.your_function_name)
]
if the request is first_app/catalog then ur first_app urls.py file should be something like this in url patterns
urlpatterns = [
path('/catalog', views.catalog, name='catalog'),
]
from django.contrib import admin
from django.urls import path, include
from first_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('first_app/', include('first_app.urls')),]
and
from django.urls import path
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
]
>>>>> if your templates files :
templates/index.html
def index(request):
webpages_list = AccessRecord.objects.order_by('date')
date_dict = {'access_records': webpages_list}
return render(request, 'index.html', context=date_dict)

Django different url going to the same page

I am trying to create 2 extra pages on my Django site, I created the first one with no problem (calendar.html) but when I try to create the second one (actionplan.html) it gives me no error, but when I access xxx/actionplan.html , it shows the calendar.html page... I cannot access xxx/actionplan.html
This is my urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static
from django.views.generic import TemplateView
from django.views.generic.detail import DetailView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
url(r'xxx', TemplateView.as_view(template_name="calendar.html")),
url(r'^xxx/$', DetailView.as_view(template_name="actionplan.html")),
url(r'^admin/', admin.site.urls),
url(r'^', include('blog.urls'), name="Blog"),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
This is my views.py:
from django.views import generic
from .models import Post
class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'index.html'
class PostDetail(generic.DetailView):
model = Post
template_name = 'post_detail.html'
class Calendar(generic.DetailView):
model = Post
template_name = 'calendar.html'
class Planoacao(generic.DetailView):
model = Post
template_name = 'actionplan.html'
I have tried:
url(r'^xxx/$', DetailView.as_view(template_name="actionplan.html")),
url(r'^xxx', DetailView.as_view(template_name="actionplan.html")),
url(r'^xxx$', DetailView.as_view(template_name="actionplan.html")),
url(r'xxx', DetailView.as_view(template_name="actionplan.html")),
I am officially out of ideas now... can anyone spot a problem?
The problem is, in your urlpatterns you put the same url two times:
url(r'xxx', TemplateView.as_view(template_name="calendar.html")),
url(r'^xxx/$', DetailView.as_view(template_name="actionplan.html")),
You should add a different url for the two views,
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
url(r'^xxx/plan$', DetailView.as_view(template_name="actionplan.html")),
url(r'xxx', TemplateView.as_view(template_name="calendar.html")),
url(r'^admin/', admin.site.urls),
url(r'^', include('blog.urls'), name="Blog"),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
and you should use path no url and not both
You've given them the same url essentially, yoursite.com/xxx, you could reorder them and put the one with the slash first and that might work but then that would make it a nightmare if you use django's APPEND_SLASH setting.
To fix, make your urls unique
The actionplan.html has nothing to do with your url, its "working" only because the regex for the calendar is just looking for xxx in the given url

Page not found in Django

I'm following this tutorial to learn Django. I'm a total starter.
My urls.py file has following code
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
My views.py file is
from django.shortcuts import render
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
When i try to access th url http://127.0.0.1:8000/polls/ in my system it gives a page not found message. What am i doing wrong?
Is it a problem with the version difference?
Here is the screenshot of the error
This error comes if your urls.py file in polls directory does not have this pattern. Create a file urls.py your polls directory and add following url pattern
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
)
Ok, so if you want to use /polls/ with this urls,
your polls.urls should look similar to this ( for django1.8):
from django.conf.urls import url
from polls.views import BasePollView, AnotherPollView
urlpatterns = [
url(r'^$', BasePollView.as_view()),
url(r'^another-url/$', AnotherPollView.as_view())
]
/polls/ -> BasePollView
/polls/another-url/ -> AnotherPollView

Categories

Resources