Writing - pai-plznw4me/django-initializer GitHub Wiki

WEB 개발 ν‘œμ€€

  • Django DateField

    • Form λ‚΄ Meta class 에 μ•„λž˜ widget μΆ”κ°€

    • widgets = {
          '{{fieldname}}': forms.widgets.DateInput(attrs={'type': 'date'})
      }
    • # λ¬Έμ„œ μ—…λ‘œλ“œ μ‹œκ°„
      # 파일 μ—…λ‘œλ“œμ‹œ μ•„λž˜μ™€ 같은 ν•„λ“œ μΆ”κ°€
      upload_date = models.DateField(auto_now_add=True)

Django ImageField

  • Django Graph

  • Django table

  • Django BI

  • File management system

    • νŒŒμΌμ„ μ—¬λŸ¬κ°œ λ§Œλ“€μ–΄μ•Ό ν•œλ‹€κ³  ν•˜μž μ–΄λ–»κ²Œ 관리 ν•  것인가?
    • 파일 관리 DB을 λ§Œλ“€μ–΄μ„œ 관리해야 ν•œλ‹€.
  • CharField

    • choice

    • 포맷 κ°•μ œν•˜κΈ°

      from django.db import models
      from django.core.exceptions import ValidationError
      from django.core.validators import RegexValidator
      
      def validate_version_format(value):
          version_regex = r'^v\d+\.\d+\.\d+$'
          if not re.match(version_regex, value):
              raise ValidationError('Invalid version format. Use the format "vX.X.X".')
      
      class YourModel(models.Model):
          version = models.CharField(max_length=10, validators=[validate_version_format])
          # λ‹€λ₯Έ ν•„λ“œλ“€...
  • pms 을 λ§Œλ“ λ‹€κ³  ν•˜μž

    • λ‚˜λŠ” κ°œλ°œν• λ•Œ μ—¬λŸ¬ λΉ„μ •ν˜•μ˜ 정보듀을 μ§€μ†μ μœΌλ‘œ μΆ”κ°€ν•΄μ•Ό ν–ˆλ‹€.
    • pms db μ•ˆμ— 넣을렀고 ν–ˆμ§€λ§Œ κ²°κ΅­ file db 을 λ”°λ‘œ λ§Œλ“€μ—ˆλ‹€.
  • μ½”λ“œ 자체λ₯Ό λ³€κ²½ ν•˜λŠ” 방법

    • shell

    • ast

      import ast
      
      file_path = "a.py"
      app_name = "helloworld"
      
      # 파일 읽기
      with open(file_path, "r") as file:
          code = file.read()
      
      # μ½”λ“œ νŒŒμ‹±
      tree = ast.parse(code)
      
      # INSTALLED_APPSλ₯Ό μ°Ύμ•„μ„œ λ…Έλ“œμ— 'helloworld' μΆ”κ°€
      for node in ast.walk(tree):
          if isinstance(node, ast.Assign):
              for target in node.targets:
                  if isinstance(target, ast.Name) and target.id == 'INSTALLED_APPS':
                      if isinstance(node.value, ast.List):
                          node.value.elts.append(ast.Str(s=app_name))
      
      # μˆ˜μ •λœ μ½”λ“œ 생성
      modified_code = compile(tree, filename=file_path, mode='exec')
      
      # νŒŒμΌμ— μˆ˜μ •λœ μ½”λ“œ μ €μž₯
      with open(file_path, "w") as file:
          file.write(modified_code)
      
      print(f"App '{app_name}' added to INSTALLED_APPS in {file_path}.")
  • Model - form - view - template 개발

    • Model

      from django.db import models
      
      # Create your models here.
      class File(models.Model):
          # 파일 이름
          name = models.CharField(max_length=100)
          # λ¬Έμ„œ μ—…λ‘œλ“œ μ‹œκ°„
          upload_date = models.DateField()
          # λ¬Έμ„œ 생성 μ‹œκ°„
          creation_date = models.DateField()
          # λ¬Έμ„œ 버전
          name = models.CharField(max_length=100)
          # Project
          PROJECT_CHOICES = [
              ('A', 'AAA'),
              ('B', 'BBB'),
              ('C', 'CCC'),
          ]
          project = models.CharField(max_length=10, choices=PROJECT_CHOICES)
          # file content
          filecontent = models.FileField()
          # image content
          imagecontent= models.ImageField()
    • Form

      class CreateFileForm(forms.ModelForm):
          class Meta:
              model = File
              fields = ['name',
                        'upload_date',
                        'creation_date',
                        'name',
                        'project',
                        'filecontent',
                        'imagecontent']
    • View

    • Templates

      <!-- templates/registration/login.html -->
      <h2>Signup</h2>
      <form action="" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Signup</button>
      </form>
      {{ errors }}

Convention

  • FileCreationForm {λͺ¨λΈλͺ…}{λͺ©μ λͺ…}Form
⚠️ **GitHub.com Fallback** ⚠️