12- ساخت اولین اپلیکیشن در جنگو(3) (صفحه ادمین):

۰۷ تیر ۱۴۰۴
0 دیدگاه

درسنامه: آموزش ویوها (Views) در جنگو – بخش سوم از ساخت اولین اپلیکیشن

توضیحات متا (Meta Description):

 

در این آموزش از سری ساخت اولین اپلیکیشن با جنگو، به مبحث ویوها (Views) می‌پردازیم و نحوه‌ی اتصال URL‌ها به توابع ویو را بررسی می‌کنیم. با ما همراه باشید تا صفحه‌ای پویا و قابل تعامل برای کاربران ایجاد کنید.


 

 مقدمه: ویوها در جنگو چیستند؟

 

در جنگو، ویو (View) همان چیزی‌ست که مسئول پاسخ‌دهی به درخواست‌های کاربران است. هر ویو می‌تواند:

  •  
  • اطلاعاتی از دیتابیس بخواند

  •  
  • از یک تمپلیت برای نمایش داده‌ها استفاده کند

  •  
  • یا حتی یک فایل PDF، JSON یا ZIP بسازد!


 

 

 URL و ویوها: مسیریابی چطور کار می‌کند؟

 

در جنگو، مسیرها (URL) به ویوها وصل می‌شوند از طریق چیزی به نام URLconf.

مثال:

				
					# polls/urls.py
path("<int:question_id>/", views.detail, name="detail")

				
			

اگر کاربر وارد آدرس /polls/34/ شود، جنگو تابع detail(request, question_id=34) را اجرا می‌کند.

 کلیدواژه مهم سئو: “مسیر‌یابی در جنگو”، “URLconf”، “views در Django”


 

 تعریف ویوهای اولیه برای اپلیکیشن نظرسنجی

 

در فایل polls/views.py ویوهای زیر را تعریف می‌کنیم:

				
					from django.http import HttpResponse

def detail(request, question_id):
    return HttpResponse(f"You're looking at question {question_id}.")

def results(request, question_id):
    return HttpResponse(f"You're looking at the results of question {question_id}.")

def vote(request, question_id):
    return HttpResponse(f"You're voting on question {question_id}.")

				
			

 

اتصال URL به ویوها در polls/urls.py:

				
					from django.urls import path
from . import views

app_name = "polls"
urlpatterns = [
    path("", views.index, name="index"),
    path("<int:question_id>/", views.detail, name="detail"),
    path("<int:question_id>/results/", views.results, name="results"),
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

				
			

ویوی index – نمایش آخرین سوال‌ها

قدم ۱: تعریف ویو

				
					from django.shortcuts import render
from .models import Question

def index(request):
    latest_question_list = Question.objects.order_by("-pub_date")[:5]
    context = {"latest_question_list": latest_question_list}
    return render(request, "polls/index.html", context)

				
			

 

قدم ۲: ساخت تمپلیت polls/templates/polls/index.html

				
					{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

				
			

برای جلوگیری از تکرار و افزایش سئو، از namespace در URLها استفاده می‌کنیم: polls:detail


 

 

جلوگیری از خطا: 404 Not Found

روش اول:

				
					from django.http import Http404

def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request, "polls/detail.html", {"question": question})

				
			

 

روش بهتر (شورتکات):

				
					from django.shortcuts import get_object_or_404

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, "polls/detail.html", {"question": question})

				
			

 

تمپلیت polls/detail.html

				
					<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

				
			

جمع‌بندی درس

 

در این درس با مفاهیم زیر آشنا شدیم:

 

  • تعریف ویو در جنگو

  •  
  • اتصال URL به ویو

  •  
  • استفاده از تمپلیت برای جدا کردن منطق از طراحی

  •  
  • مدیریت خطا با 404

  •  
  • استفاده از render() و get_object_or_404() برای کدنویسی خواناتر

0 دیدگاه