ycliper

Популярное

Музыка Кино и Анимация Автомобили Животные Спорт Путешествия Игры Юмор

Интересные видео

2025 Сериалы Трейлеры Новости Как сделать Видеоуроки Diy своими руками

Топ запросов

смотреть а4 schoolboy runaway турецкий сериал смотреть мультфильмы эдисон
Скачать

Our First Django View

First Django View

Django view

django render

django request and response

How to query database in django

django first view

django rendering views

django views

django views.py

how to load a view in django template

15.our first django view

access views.py django

adding views in django

def post def get django

django cms tutorial

django context and views

django first html

django first template view

django first view render

django for loop views html

Автор: Code master

Загружено: 2016-10-11

Просмотров: 3591

Описание: Lesson 16 - Building Are First Django View
------------------------------------------
In this tutorial, we will build our first django view which will return all our blog post. If you are fimilar with wordpress or any blog type of application this is where we will see a list of blog post. Well atleast that is what we are going for in this tutorial.

1. Open up views.py. The views.py file recieves a request from the user and our code will return a response. So in short views handle request and response.

2. In the views.py file we see 'from django.shortcuts import render'. Render takes a request and returns the approiate template. So if the user request a list of blog post then render will take that response and return a template. You can think of a template as a html document which may contain some attentional context.

3. Now let's start creating our view. First we need access to our database so just like before we are going to add a import statement to access our database.
'from .models import Post'

4. Now we are going to create a function that will take a users request and return a response. So we will create a function called list_of_post.
'def list_of_post(request):'

5. Now what do we want to display? Well we want to display all our current blog post. How would we do that? We in the previous tutorial where I stressed how important it is to understand how to query the database this where it comes in. This next line of code will look very fimilar. Let's add this line
'post = Post.objects.all()' Post now represents all post in the database.

6. now we need to render the post per the request. How do we do this? We need to provide two arguments request and template name and then a third one we will use is often referred to as context.
request is coming from the url.py file bear with me on this one.
template name is going to be the location and name of html file we want to return.
context is going to all our post which is represent by the variable post.

putting it all together.
return render(request, 'blog/post/list_of_post.html', {'post': post})

Now that is how it should look per django documentation now I like to clean up my views a little more so they more uniformed. So what we are going to do is take the template and store that in a variable and the same with context. Final code will look like this.

def list_of_post(request):
post = Post.objects.all()
template = 'blog/post/list_of_post.html'
context = {'post': post}
return render(request, template, context)

Now we have create a first view there is not much we can do with it until we grab the users request and build a template to return the user.

Не удается загрузить Youtube-плеер. Проверьте блокировку Youtube в вашей сети.
Повторяем попытку...
Our First Django View

Поделиться в:

Доступные форматы для скачивания:

Скачать видео

  • Информация по загрузке:

Скачать аудио

Похожие видео

© 2025 ycliper. Все права защищены.



  • Контакты
  • О нас
  • Политика конфиденциальности



Контакты для правообладателей: [email protected]