Form and ModelForm

.order_by()

pk μ—­μˆœμœΌλ‘œ μ •λ ¬

articles = Article.objects.order_by('-pk')

ModelForm

ModelForm μ •μ˜ν•˜κΈ°

forms.py

from django import forms
from .models import Article

class ArticleForm(forms.ModelForm):
    title = forms.CharField(
        max_length=100,
        label='Title',
        help_text='Your title must be no more than 100 characters in length',
        widget=forms.TextInput(
            attrs={
                'class':'my_input',
                'placeholder': "What's on your mind?"
            }
        )
    )
    content = forms.CharField(
        label='Content',
        help_text='Jot down random musings and thoughts',
        widget=forms.Textarea(
            attrs={
                'row':5,
                'col':50,
            }
        )
    )
    class Meta:
        model = Article
        # λ‹€ λ•Œλ €λ°•μ•„
        fields = '__all__'

Viewμ—μ„œ ModelFrom ν™œμš©

view.py

request.resolver_match.url_name 으둜 쑰건에 λ”°λ₯Έ 처리

form.html

  • λΆ„κΈ°μ˜ 기쀀은 url_name 이닀

  • path둜 ν•˜λ©΄, url이 λ°”λ€” λ•Œλ§ˆλ‹€ λ°”κΏ”μ€˜μ•Ό ν•œλ‹€!

loop or bootstrap4 ν™œμš©ν•˜μ—¬ 좜λ ₯ν•˜κΈ°

form.html

shell λ“€μ–΄κ°€κΈ°

shell

p tag 둜 이루어진 inputλ“€μ˜ μ§‘ν•©

table둜 이루어진 inputλ“€μ˜ μ§‘ν•©

Looping over the form’s fields

If you’re using the same HTML for each of your form fields, you can reduce duplicate code by looping through each field in turn using a `

` loop:

Form rendering options

There are other output options though for the / pairs:

  • {{ form.as_table }} will render them as table cells wrapped in tags

  • {{ form.as_p }} will render them wrapped in `` tags

  • {{ form.as_ul }} will render them wrapped in `` tags

Note that you’ll have to provide the surrounding or elements yourself.

Here’s the output of {{ form.as_p }} for our ContactForm instance:

Django Bootstrap μ‚¬μš©ν•˜κΈ°

Install

settings.py μˆ˜μ •

base.html에 bootstrap μ‚¬μš©ν• κ±°λΌκ³  μ„ μ–Έ

detail.html - bootstrap μ μš©λ˜λŠ” template듀에도 μ„ μ–Έ

+

Git

νŠΉμ • commit을 κΈ°μ€€μœΌλ‘œ λŒμ•„κ°€κΈ°

test branch λ§Œλ“€λ©΄μ„œ μ΄λ™ν•˜κΈ°

reference log 확인

어디에 head μžˆλŠ”μ§€ 확인

λ‹€μ‹œ test branch둜 λŒμ•„κ°€κΈ°

Last updated