From 73c721021747c2d0c854ccdaf95e138b5bd31c67 Mon Sep 17 00:00:00 2001 From: Saeid Javadi Date: Tue, 19 Nov 2024 19:38:58 +0330 Subject: [PATCH 1/3] edit tag --- source/_posts/django.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/_posts/django.md b/source/_posts/django.md index f42e807..83199a7 100644 --- a/source/_posts/django.md +++ b/source/_posts/django.md @@ -653,7 +653,7 @@ app_name/ - And in your template you would use the following: -```python +```html {% load basetags %} ``` From 0c988af755fda8d0eb2cc979becabafdc5a9f6b5 Mon Sep 17 00:00:00 2001 From: Saeid Javadi Date: Mon, 25 Nov 2024 12:49:08 +0330 Subject: [PATCH 2/3] update forms --- source/_posts/django.md | 198 ++++++++++++++++++++++------------------ 1 file changed, 110 insertions(+), 88 deletions(-) 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 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' %} -

Hello Admin

+

Hello Admin

{% elif user.username = 'john' %} -

Hello John Doe

+

Hello John Doe

{% else %} -

Hello User

+

Hello User

{% endif %} {% for product in products %} -

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" %} - -

Hello {{ name }}!
- -{% endwith %} + + {% with name="World" %} + +
Hello {{ name }}!
+ + {% endwith %} - -{% load i18n %} -{% trans "This is the title." %} - -{% trans object.title %} + + {% load i18n %} + {% trans "This is the title." %} + + {% trans object.title %} - - + + - - - + - - + + - - - - + + + - - + + - - + - - + + +

``` ### Custom Template Tags and Filters @@ -732,7 +751,7 @@ def site_email(request): 'OPTIONS': { 'context_processors': [ … - 'app_name.context_processors.site_email', + 'app_name.context_processors.site_email', # New context processor here … ], @@ -866,8 +885,16 @@ class ArticleForm(ModelForm): {% csrf_token %} {{ form }} +``` -{% load crispy_forms_tags %} {{ form|crispy }} {{ form.email|as_crispy_field }} +- NOTE: If the form contains a file field, your form MUST contain `enctype="multipart/form-data"`, eg: + +```django +
+ {% csrf_token %} + {{ form.as_p }} + +
``` ##### A Tailwind CSS template pack for the wonderful django-crispy-forms. @@ -918,7 +945,6 @@ def clean(self): ### Displaying messages - ```python # Message tags # debug, info, success, warning and error @@ -929,11 +955,7 @@ messages.error(request, 'Login error') ```html -{% if messages %} - {% for message in messages %} - {% message %} {% message.tags %} - {% endfor %} -{% endif %} +{% if messages %} {% for message in messages %} {% message %} {% message.tags %} {% endfor %} {% endif %} ``` ## User Model @@ -2180,4 +2202,4 @@ If you change the Nginx server block configuration, test the configuration and t ```bash sudo nginx -t && sudo systemctl restart nginx -``` +``` \ No newline at end of file From 543a2936003d7ceb1c6af2a1b401c7f875cfeba7 Mon Sep 17 00:00:00 2001 From: Saeid Javadi Date: Mon, 25 Nov 2024 13:00:38 +0330 Subject: [PATCH 3/3] Update django.md --- source/_posts/django.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/_posts/django.md b/source/_posts/django.md index dc37c91..38841df 100644 --- a/source/_posts/django.md +++ b/source/_posts/django.md @@ -889,7 +889,7 @@ class ArticleForm(ModelForm): - NOTE: If the form contains a file field, your form MUST contain `enctype="multipart/form-data"`, eg: -```django +```html
{% csrf_token %} {{ form.as_p }} @@ -2202,4 +2202,4 @@ If you change the Nginx server block configuration, test the configuration and t ```bash sudo nginx -t && sudo systemctl restart nginx -``` \ No newline at end of file +```