standard create - pai-plznw4me/django-initializer GitHub Wiki

ํ‘œ์ค€ Create Page ๊ฐ€์ด๋“œ

What?

  • ํ‘œ์ค€ ์ƒ์„ฑ๋Š” Target ๋ชจ๋ธ record ์„ ์ถ”๊ฐ€ํ•˜๋Š”๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
  • ํ‘œ์ค€ create page ๋Š” standard_create() ์„ ํ†ตํ•ด ์ƒ์„ฑ๋œ ์ฝ”๋“œ ๋ฐ HTML ์„ ์˜๋ฏธํ•œ๋‹ค.
def standard_create(request, template_name, form_class, redirect_view, redirect_path_variables, callback,
                    **callback_kwargs):
    """
    ํ‘œ์ค€ ์ƒ์„ฑ ํผ ์ฒ˜๋ฆฌ ๋ทฐ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํผ ์ฒ˜๋ฆฌ ํ›„ ๋ฆฌ๋””๋ ‰์…˜์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

    :param request: HttpRequest ๊ฐ์ฒด
    :param template_name: ๋ Œ๋”๋ง์— ์‚ฌ์šฉํ•  ํ…œํ”Œ๋ฆฟ ์ด๋ฆ„
    :param form_class: ์‚ฌ์šฉํ•  ํผ ํด๋ž˜์Šค
    :param redirect_view: ํผ ์ฒ˜๋ฆฌ ์™„๋ฃŒ ํ›„ ์ด๋™ํ•  ๋ทฐ ์ด๋ฆ„
    :param redirect_path_variables: ๋ฆฌ๋””๋ ‰์…˜ ์‹œ ์ „๋‹ฌํ•  ์ถ”๊ฐ€์ ์ธ ๊ฒฝ๋กœ ๋ณ€์ˆ˜๋“ค (๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ)
    :param callback: ์„ ํƒ์ ์ธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ (ํ•จ์ˆ˜ ํ˜น์€ ๋ฉ”์„œ๋“œ)
    :param callback_kwargs: ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•  ์ถ”๊ฐ€ ํ‚ค์›Œ๋“œ ์ธ์ž๋“ค
    :return: HttpResponse ๊ฐ์ฒด
    """
    if request.method == 'POST':
        # POST ์š”์ฒญ ์ฒ˜๋ฆฌ
        form = form_class(request.POST, request.FILES)  # POST ๋ฐ์ดํ„ฐ ๋ฐ ํŒŒ์ผ ์ฒ˜๋ฆฌ
        # ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ํ†ต๊ณผ ์‹œ
        if form.is_valid():
            valid_inst = form.save(commit=False)
            # ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœ
            if callback:

                callback(request=request,
                         template_name=template_name,
                         form_class=form_class,
                         redirect_view=redirect_view,
                         redirect_path_variables=redirect_path_variables,
                         valid_inst=valid_inst,
                         **callback_kwargs)
            # ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ
            valid_inst.save()

            # ๋ฆฌ๋””๋ ‰์…˜
            if redirect_path_variables:  # redirect_path_variables ์ด None ์ด ์•„๋‹ˆ๋ฉด
                return redirect(redirect_view, **redirect_path_variables)
            else:
                return redirect(redirect_view)
        # ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์‹คํŒจ์‹œ
        else:
            content = render(request, template_name=template_name, context={'form': form}).content.decode('utf-8')
            ret_html = add_content(request, 'doctris', content)
            return HttpResponse(ret_html)
    elif request.method == 'GET':  # GET ์š”์ฒญ ์ฒ˜๋ฆฌ
        form = form_class()  # <-- FILES ์™€ ๊ฐ™์ด ์ž…๋ ฅ์ด ๋˜์–ด์•ผ ํ•œ๋‹ค.

        # ํผ์„ ๋ Œ๋”๋งํ•˜๊ณ  ์ถ”๊ฐ€ ์ฝ˜ํ…์ธ  ์ƒ์„ฑ
        added_contents = []
        content = render(request, template_name=template_name, context={'form': form}).content.decode('utf-8')
        added_contents.append(content)

        # ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœ
        if callback:
            callback(request=request,
                     template_name=template_name,
                     form_class=form_class,
                     redirect_view=redirect_view,
                     redirect_path_variables=redirect_path_variables,
                     added_contents=added_contents,
                     **callback_kwargs)
        # ์ตœ์ข… ๋ Œ๋”๋ง
        ret_html = add_content(request, 'doctris', *added_contents)
        return HttpResponse(ret_html)

![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-26 แ„‹แ…ฉแ„’แ…ฎ 2.05.12](/Users/kimseongjung/Desktop/แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-26 แ„‹แ…ฉแ„’แ…ฎ 2.05.12.png)

์†์„ฑ

  • CreateForm ์„ ํ™œ์šฉํ•ด Record ์ถ”๊ฐ€์‹œ ํ•„์š”ํ•œ ํ•„๋“œ ํ•ญ๋ชฉ์„ ๊ฐ€์ ธ์˜จ๋‹ค.

  • ์ถ”๊ฐ€๊ฐ€ ๋ชจ๋‘ ๋๋‚˜๋ฉด redirect ๋ฅผ ํ†ตํ•ด ์‹ ํ˜ธ๋ฅผ ์†ก์‹ ํ•œ๋‹ค.

  • callback ํ•จ์ˆ˜๋Š” GET, POST ๋ชจ๋‘ ์‹คํ–‰๋œ๋‹ค.

    if method == 'GET' or method == 'POST' ์„ ํ†ตํ•ด์„œ ์ž‘์„ฑ

How to use ?

# 2๋ฒˆ์งธ ์˜ˆ์ œ
def create(request):
    """
    ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ทฐ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

    :param request: HttpRequest ๊ฐ์ฒด
    :return: HttpResponse ๊ฐ์ฒด
    """
    def _callback(**kwargs):
        if kwargs['request'].method == 'GET':
            title = h_tag(2, 'ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ')
            kwargs['added_contents'][0] = title + card_row((kwargs['added_contents'][0], 12))

    return standard_create(request, 'standard/create.html', ProjectFileCreateForm, 'project:detail', {}, _callback)

  
# 2๋ฒˆ์งธ ์˜ˆ์ œ
def projectfile_create(request):
  """
  ํ”„๋กœ์ ํŠธ ์‚ฐ์ถœ๋ฌผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ทฐ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

  :param request: HttpRequest ๊ฐ์ฒด
  :return: HttpResponse ๊ฐ์ฒด
  """
  def _callback(**kwargs):
      if kwargs['request'].method == 'POST':
          valid_inst = kwargs['valid_inst']
          redirect_path_variables = kwargs['redirect_path_variables']
          # ์˜† ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ฝ”๋“œ=> redirect(project:detail, id=valid_inst.project_id)
          redirect_path_variables['id'] = valid_inst.project_id
      if kwargs['request'].method == 'GET':
          title = h_tag(2, 'ํ”„๋กœ์ ํŠธ ์‚ฐ์ถœ๋ฌผ ์ƒ์„ฑ')
          kwargs['added_contents'][0] = title + card_row((kwargs['added_contents'][0], 12))

  return standard_create(request, 'standard/create.html', ProjectFileCreateForm, 'project:detail', {},
                         _callback)

  1. standard_index ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  2. ๋ชฉ๋ก์„ ๋ณด์—ฌ์ฃผ๊ณ ์ž ํ•˜๋Š” ๋ชจ๋ธ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ชฉ๋ก ํ•„ํ„ฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  4. Form field ์„ ํ†ตํ•ด ์–ด๋–ค ํ•„๋“œ๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„์ง€ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค
  5. url path ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  6. design (base) ํ˜•ํƒœ๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
  7. callback function ์„ ์ œ๊ณตํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๊ฐ์ฒด๋ฅผ ์ปจํŠธ๋กค ํ• ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

Callback function

์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด callback function ์„ ํ†ตํ•ด์„œ ์šฐ๋ฆฌ๋Š” ๋‹ค์–‘ํ•œ ์ž‘์—…๋“ค์„ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น function ์—์„œ์˜ callback function ์€ ๋”ฑํžˆ ์ •ํ•ด์ง„ ๋ชฉ์ ์€ ์—†์ง€๋งŒ ๋Œ€ํ‘œ์ ์œผ๋กœ 2๊ฐ€์ง€ ๋ชฉ์ ์„ ๊ฐ€์ง€๊ณ  ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  1. POST ์ผ ๋•Œ, ์ƒ์„ฑ๋œ instance ์„ ํ™œ์šฉํ•ด ์ž‘์—…์„ ํ• ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

  2. POST ์ผ ๋•Œ, redirect ์‹œ ํ•„์š”ํ•œ ํ‚ค์›Œ๋“œ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ• ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  3. GET ์ผ ๋•Œ, content design ์„ ์ž…ํžˆ๊ฑฐ๋‚˜ html ์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ž‘์—…๋“ค์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

    1. ์ œ๋ชฉ ์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ž‘์—…๋“ค์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

      title = h_tag(2, 'ํ”„๋กœ์ ํŠธ ๋ชฉ๋ก')
      
    2. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” curd_table_html ์— div class=card ์— curd_table_html ์„ ์‚ฝ์ž…ํ•˜๋Š” ์ž‘์—…๋“ค์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

      added_contents = kwargs['added_contents']
      added_contents[0] = title + card_row((added_contents[0], 12)
      

โš ๏ธ ์ถ”๊ฐ€์ ์œผ๋กœ added_contents ์— ์ถ”๊ฐ€๋œ ์ž๋ฃŒ๋Š” ์ˆœ์„œ๋Œ€๋กœ ํ™”๋ฉด์— ๋žœ๋”๋ง ๋ฉ๋‹ˆ๋‹ค.