Best VPN Service (English Banners)

GeekBuying.com

Using Django to Build a Website (Mac) 5

Alibaba Cloud
Autonomous Standing Desk

System Mechanic® Ultimate Defense™
Save up to 50% on ANSI Standards Packages

code

Dynamic URL Routing

STEP 1 DEFINE dynamic_lookup_view IN views.py

##Dynamic URL Routing
def dynamic_lookup_view(request, my_id):
obj = Product.objects.get(id=my_id)
context = {
“object”: obj
}
return render(request, “products/product_detail.html”, context)

STEP2 ADD dynamic_lookup_view INTO urls.py

IK Multimedia's T-RackS 5

from products.views import (
product_detail_view,
product_create_view,
render_initial_data,
dynamic_lookup_view
)

path(‘initial/’, render_initial_data),
path(‘products/<int:my_id>/’, dynamic_lookup_view, name =’product’),
path(‘admin/’, admin.site.urls),

STEP 3 CHECK http://127.0.0.1:8000/products/1/ OR http://127.0.0.1:8000/products/my_id/

Handle DoesNotExist 2:54:26

1 OF 2 WAYS: ADD get_object_or_404 IN views.py:

from django.shortcuts import render, get_object_or_404

from .models import Product

def dynamic_lookup_view(request, my_id):
#obj = Product.objects.get(id=my_id)
obj = get_object_or_404(Product, id=my_id)
context = {
“object”: obj
}
return render(request, “products/product_detail.html”, context)

Screenshot 2019-11-21 at 11.52.46

2 OF 2 WAYS: ADD from django.http import Http404 IN views.py:

Autonomous Standing Desk

def dynamic_lookup_view(request, my_id):
obj = Product.objects.get(id=my_id)
# obj = get_object_or_404(Product, id=my_id)
try:
obj = Product.objects.get(id=my_id)
except Product.DoesNotExist:
raise Http404
context = {
“object”: obj
}
return render(request, “products/product_detail.html”, context)

Screenshot 2019-11-21 at 11.49.45

Delete and Confirm

STEP 1 DEFINE product_delete_view IN views.py

def product_delete_view(request,my_id):
obj = get_object_or_404(Product,id=my_id)
# POST request
if request.method == “POST”:
# confirming to delete
obj.delete()
context = {
“object”: obj
}
return render(request, “products/product_delete.html”, context)

STEP 2 SET UP TEMPLATE product_delete.html

{% extends ‘base.html’ %}

{% block content %}

<form action=’.’ method=’POST’>{% csrf_token %}
<h1>确定删除产品吗?”{{ object.title }}”?</h1>
<p><input type=’submit’ value=’Yes’ /> <a href=’../’>Cancel</a></p>
</form>
{% endlock %}

STEP 3 ADD URL IN urls.py

from products.views import (
product_detail_view,
product_create_view,
render_initial_data,
dynamic_lookup_view,
product_delete_view
)

path(‘products/<int:my_id>/’, dynamic_lookup_view, name =’product’),
path(‘products/<int:my_id/delete/’, product_delete_view, name=’product_delete’),
path(‘admin/’, admin.site.urls),

Direct after Deletion 2:58:00

ADD redirect IN views.py

from django.shortcuts import render, get_object_or_404, redirect

ADD return redirect(‘../’)

def product_delete_view(request,my_id):
obj = get_object_or_404(Product,id=my_id)
# POST request
if request.method == “POST”:
# confirming to delete
obj.delete()
return redirect(‘../’)
context = {
“object”: obj
}
return render(request, “products/product_delete.html”, context)

View of a List of Database Objects

STEP 1 DEFINE product_list_view IN views.py

def product_list_view(request):
queryset = Product.objects.all()
context = {
“object_list”: queryset
}
return render(request, “products/product_list.html”, context)

STEP 2 EDIT products/product_list.html

{% extends ‘base.html’ %}

{% block content %}

{{ object_list }}

{% for instance in object_list %}
<p>{{ instance.id }} – {{ instance.title }}</p>
{% endfor %}

{% endblock %}

STEP 3 EDIT from products.views import AND urlpatterns IN urls.py

from products.views import (
product_detail_view,
product_create_view,
render_initial_data,
dynamic_lookup_view,
product_delete_view,
product_list_view
)

urlpatterns = [
#path(”, views.home_view, name=’home’),
path(”, home_view, name=’home’),
path(‘contact/’, contact_view),
path(‘about/’, about_view),
path(‘social/’, social_view),
path(‘product/’, product_detail_view, name=’product-detail’),
path(‘create/’, product_create_view),
path(‘initial/’, render_initial_data),
path(‘products/’, product_list_view, name=’product-list’),
path(‘products/<int:my_id>/’, dynamic_lookup_view, name =’product’),
path(‘products/<int:my_id>/delete/’, product_delete_view, name=’product_delete’),
path(‘admin/’, admin.site.urls),
]

Dynamic Linking of URLs

STEP 1 DEFINE get_abosolute_url IN models.py

def get_absolute_url(self):
return f”/products/{self.id}/”

STEP 2 CHANGE STATIC URL TO {{ instance.get_absolute_url}}

{% extends ‘base.html’ %}

{% block content %}

{{ object_list }}

{% for instance in object_list %}
<!– <p>{{ instance.id }} – {{ instance.title }}</p> –>
<p>{{ instance.id }} – <a href='{{ instance.get_absolute_url}}’>{{ instance.title}}</a>
</p>
{% endfor %}

{% endblock %}

Django URLs Reverse

from django.urls import reverse AND CHANGE RETURN OF get_absolute_url IN models.py :

from django.db import models
from django.urls import reverse

# Create your models here.
class Product(models.Model):
title = models.CharField(max_length=120) #max_length = required
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=1000)
summary = models.TextField(blank=False, null=False)#default=’Must Buy’)
featured = models.BooleanField(default=True) #null=True, default=True
def get_absolute_url(self):
# return f”/products/{self.id}/”
return reverse(“product-detail”, kwargs={“id”: self.id})

In App URLs and Namespacing

STEP 1 SET UP A NEW urls.py IN products APP; COPY FROM urls.py IN trydjango

from django.urls import path

from products.views import (
product_detail_view,
product_create_view,
render_initial_data,
dynamic_lookup_view,
product_delete_view,
product_list_view,
product_update_view
)

app_name = ‘products’ #namespacing
urlpatterns = [
path(‘initial/’, render_initial_data),
path(”, product_list_view, name=’product-list’),
path(‘create/’, product_create_view),
path(‘<int:my_id>/’, dynamic_lookup_view, name =’product-detail’),
path(‘<int:my_id>/update/’, product_update_view, name=’product-update’),
path(‘<int:my_id>/delete/’, product_delete_view, name=’product-delete’),
]

STEP 2 TAKE OUT THE URLS MOVED TO THE APP:

“””trydjango URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path(”, views.home, name=’home’)
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path(”, Home.as_view(), name=’home’)
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path(‘blog/’, include(‘blog.urls’))
“””
from django.contrib import admin
# from django.urls import path
from django.urls import include, path

from pages import views
from pages.views import home_view
from pages.views import contact_view
from pages.views import about_view
from pages.views import social_view
# from products.views import (
# product_detail_view,
# product_create_view,
# render_initial_data,
# dynamic_lookup_view,
# product_delete_view,
# product_list_view,
# product_update_view,
# )

urlpatterns = [
#path(”, views.home_view, name=’home’),
path(”, home_view, name=’home’),
path(‘contact/’, contact_view),
path(‘about/’, about_view),
path(‘social/’, social_view),
path(‘admin/’, admin.site.urls),
# path(‘product/’, product_detail_view, name=’product-detail’),
# path(‘initial/’, render_initial_data),
# path(‘products/’, product_list_view, name=’product-list’),
# path(‘products/create/’, product_create_view),
# path(‘products/<int:my_id>/’, dynamic_lookup_view, name =’product-detail’),
# path(‘products/<int:my_id>/update/’, product_update_view, name=’product-update’),
# path(‘products/<int:my_id>/delete/’, product_delete_view, name=’product-delete’),
path(‘products/’, include(‘products.urls’)),
]

STEP 3 ADD THE NAMESPACE NAME INTO REVERSE URL IN models.py

def get_absolute_url(self):
# return f”/products/{self.id}/”
return reverse(“products:product-detail”, kwargs={“my_id”: self.id})

20191121-django

Class Based Views – ListView

 

Class Based Views – CreateView

 

 

nordvpn