As the title says. Add things that you think are faster to write down than to look up
| command | Contents | 
|---|---|
| python manage.py inspectdb [Table name] | Create a model of an existing DB | 
url The structure of the file
prj -app -views _init_.py sample1.py sample2.py
Note that it is a code when it looks like (I will add it in the comment out for the time being)
urls.py
from django.urls import path
# from . import views
from .views import  sample1, sample2
app_name = 'app'
urlpatterns = [
    # path('sample1', views.sample1, name='sample1'),
    # path('sample2', views.sample2, name='sample2')
    path('sample1', sample1.method_name, name='sample1'),
    path('sample2', sample2.method_name, name='sample2')
]
Form
forms.py
import re
from django import forms
from django.forms import RadioSelect
from django.core.exceptions import ValidationError
class DocumentForm(forms.Form):
    #Just a character field
    text_Field = forms.CharField()
    #50 characters or less
    #Not required(Initial value is required)
    #Not required(Text Area format)
    text_Area = forms.CharField(max_length=50, required=False, widget=forms.Textarea)
    #Tuple of choice.(Actual value,Display value)
    #Make it a radio button(Originally pull down)
    #Initial value(Specify the actual value)
    radio_button= forms.fields.ChoiceField(
                        choices = (('1','value1'),('2','value2')),
                        widget=RadioSelect,
                        initial=2
                    )
    #File field
    file_field = forms.FileField()
    #Add a class to the element here. HTML<input name="aaa" value=... />Corresponds to the part of
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # text_Add to Field class_add class
        self.fields["text_Field"].widget.attrs['class']='add_class'
    #Make a custom validation
    # clean_[Field name]Can create validation for specific fields with
    #If the validation does not pass, you should issue an error with raise ValidationError.
    def clean_text_Field(self):
        text_Field = self.cleaned_data['text_Field']
        if re.fullmatch(r'^[a-z0-9-]+$',text_Field) == None:
            raise ValidationError("Unavailable characters are used")
        return text_Field
Test
tests.py
from django.contrib.auth.models import User
from django.test import TestCase, Client
from django.test.utils import override_settings
#The class name should start with test.
# override_Is settings a decorator that temporarily overwrites settings during testing? It seems to be a guy.
@override_settings(TEST=True)
class Test_example_View(TestCase):
    #Functions that perform tests in the class Functions that are executed before all are executed.
    #If you want to create data or need the same operation, write it here.
    def setUp(self):
        User.objects.create_user(username='tdd', email='[email protected]', password='test_pass') 
        self.client = Client()
    #A function to test. No need to put test first
    def test_redirect(self):
        response = self.client.get('/')
        #Functions that confirm the correct operation. There are various things, so I will put a little
        #If the right side is not equal to the left side, display the error message on the right
        self.assertEqual(response.status_code, 302, "Did not redirect.")
        #If the right side is not included in the left side, display the error message on the right
        self.assertIn('data', response.context, "There is no data.")
        #If the left side is not True, display the right error message
        self.assertTrue(response.context['flag'], "The flag is incorrect.")
        Recommended Posts