diff --git a/source/_posts/django.md b/source/_posts/django.md
index 83199a7..dc37c91 100644
--- a/source/_posts/django.md
+++ b/source/_posts/django.md
@@ -204,7 +204,7 @@ CSRF_COOKIE_SECURE = True
```python
# models.py
-# The id fields is automatically created by Django
+# The id fields is automaticly created by Django
# for each model that why it's not show below
from django.db import models
@@ -220,9 +220,9 @@ class Customer(models.Model)
img = models.ImageField(upload_to ='uploads/')
# Select Field (return value, display value)
TYPE_CHOICES = (
- ('Customer', 'Customer'),
- ('Supplier', 'Supplier'),
- ('Student', 'Student'),
+ ('Customer', _('Customer')),
+ ('Supplier', _('Supplier')),
+ ('Student', _('Student')),
)
type = models.CharField(choices=TYPE_CHOICES)
@@ -230,7 +230,8 @@ class Customer(models.Model)
verbose_name = "Customer"
verbose_name_plural = "Customers"
- def __str__(self): # Model string representation
+ # Model string representation
+ def __str__(self):
return self.name
# the URL that points to a resource or page on your website
@@ -238,7 +239,34 @@ class Customer(models.Model)
return reverse("customer_detail", kwargs={"pk": self.pk})
```
-### Relationship between models
+#### We can also use this method to define the ChoiceField value.
+
+```python
+ class Customer(models.Model)
+ class TypeList(models.IntegerChoices):
+ customer = 1, _('Customer'))
+ supplier = 2, _('Supplier'))
+ student = 3, _('Student'))
+ .
+ .
+ .
+ type = models.CharField(choices=TypeList.choices, default=1)
+```
+
+#### To access the ChoiceField value in the template, we need to do the following in the template:
+
+- In Django templates you can use the "`get_FOO_display()`" method, that will return the readable alias for the field, where `'FOO'` is the name of the field.
+- If the choices are stored in the variable `CHOICES` and the model field storing the selected choice is `'type'` then you can directly use
+
+```html
+
+{{ X.get_type_display }}
+
+
+{% trans X.get_type_display %}
+```
+
+#### Relationship between models
```python
# One-to-Many: (use double quotes if the entity is not yet declare) ex. "Supplier"
@@ -423,7 +451,8 @@ def show(request, id):
return render(request, 'appfolder/show.html', {'post': post})
def create(request):
- form = PostForm(request.POST or None)
+ form = PostForm(request.POST or None, request.FILES or None)
+ # When the form contains an image or file field, we should use request.FILES
if form.is_valid():
# optionally we can access form data with form.cleaned_data['first_name']
post = form.save(commit=False)
@@ -435,7 +464,7 @@ def create(request):
def edit(request, id):
post = Post.objects.get(id=id)
- form = PostForm(request.POST or None, instance=post)
+ form = PostForm(request.POST or None, request.FILES or None, instance=post)
if form.is_valid():
form.save()
return redirect('/posts')
@@ -531,8 +560,7 @@ Templates are store in `project_folder/templates` or in your Hello Admin Hello Admin Hello John Doe Hello John Doe Hello User Hello User row:
- {{ forloop.counter }} # starting index 1
- {{ forloop.counter0 }} # starting index 0
- The product name is {{ product.name }}
- The product name is {{ product.price }}
-{% endfor %}
+ row: {{ forloop.counter }} # starting index 1 {{ forloop.counter0 }} # starting index 0 The product name is {{ product.name }} The product name is {{ product.price }}
+ {% endfor %}
-
-{{ var_name }}
+
+ {{ var_name }}
-
-{{ title | lower }}
-{{ blog.post | truncatwords:50 }}
-{{ order.date | date:"D M Y" }}
-{{ list_items | slice:":3" }}
-{{ total | default:"nil" }}
+
+ {{ title | lower }} {{ blog.post | truncatwords:50 }} {{ order.date | date:"D M Y" }} {{ list_items | slice:":3" }} {{
+ total | default:"nil" }}
-
-{{ request.path }}
+
+ {{ request.path }}
-
-{% url 'posts.delete' id=post.id %}
+
+ {% url 'posts.delete' id=post.id %}
-
-{% load static %}
-{% static 'css/main.css' %}
+
+ {% load static %} {% static 'css/main.css' %}
-
-{% with name="World" %}
-
-app_folder/te
{% extends 'base.html' %}
-{% block contents %}
-{% endblock contents %}
+{% block contents %} {% endblock contents %}
{% include 'partials/header.html' %}
@@ -541,101 +569,92 @@ Templates are store in `project_folder/templates` or in your app_folder/te
{% if user.username = 'Mike' %}
-