티스토리 뷰

INTRO

Python 3.10.0

Django     3.2.8

 

- Login template 위치 수정 : sidebar.html -> topbar.html

- templates/sidebar.html : <div class="sb-sidenav-footer"> 부분 제거

....

	<div class="sb-sidenav-footer">
            <div class="small">Logged in as:</div>
            <p>{{ user.username }}</p>
        </div>
        
....

- templates/topbar.html : Navbar dropdown toggle 메뉴 부분 수정

....

<!-- Navbar-->
    <ul class="navbar-nav ms-auto ms-md-0 me-3 me-lg-4">
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="fas fa-user fa-fw"></i></a>
            <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                <li><a class="dropdown-item" href="#!">Settings</a></li>
                <li><hr class="dropdown-divider" /></li>
                <li><a class="dropdown-item" href="{% url 'accounts:logout' %}">Log-Out</a></li>
            </ul>
        </li>
    </ul>
    <div style="color:darkgray; font-size:1.2em"><B>'{{ user.username }}'</B>님 환영합니다.</div>
    
    ....

 

 

sales/customer 적용 후 미리보기

- localhost:8000/sales/customer

 

고객관리 옆 글쓰기 클릭 : localhost:8000/sales/customer_new

 

2번 공지사항 제목 클릭 : localhost:8000/sales/customer_view/1

하단 수정 클릭 : localhost:8000/sales/customer_view/1/edit

--the end--

 

 

 

-----python coding-----------------------------------------------------------------------------------

App 생성하기 : sales

- sales app 생성

$ ./manage.py startapp sales

- sales/templates 폴더 생성

- sales/templates/customer 폴더 생성

- sales/forms.py 파일 생성

- sales/urls.py 파일 생성

 

sales app 등록

- config/settings.py

....

# Application definition

INSTALLED_APPS = [
    'sales.apps.SalesConfig',
    'home.apps.HomeConfig',
    ....
]

....

 

sales model 생성

- slaes/models.py

from django.conf import settings
from django.db import models
from django.core.validators import RegexValidator


class Customers(models.Model):
	customer_id = models.AutoField(primary_key=True)
	author      = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=False)
	company     = models.CharField('회사명', max_length=64, null=False)
	site        = models.CharField('Site', max_length=64, null=True)
	team        = models.CharField('소속', max_length=64, null=True)
	position    = models.CharField('직책/직급', max_length=64, null=False)
	name        = models.CharField('이름', max_length=64, null=False)
	email       = models.EmailField('Email', max_length=128, null=False)
	phoneNumberRegex = RegexValidator(regex = r'^01([0|1|6|7|8|9]?)-?([0-9]{3,4})-?([0-9]{4})$')
	phone       = models.CharField(validators = [phoneNumberRegex], max_length = 11, unique = False)
	note        = models.TextField('메모', null=False)
	created_at  = models.DateTimeField('PO생성일',auto_now_add=True)
	updated_at  = models.DateTimeField('PO수정일',auto_now=True)
	
	def __int__(self):
		return self.customer_id

	def __str__(self):
		return self.name

 

- models.py DB 반영

$ ./manage.py makemigrations sales

$ ./manage.py migrate

 

- slaes/admin.py

from django.contrib import admin
from sales.models import Customers


class CustomersAdmin(admin.ModelAdmin):
	serch_fields = ['name']

admin.site.register(Customers, CustomersAdmin)

 

Form 만들기

- sales/forms.py

from django import forms
from sales.models import Customers


class CustomerForm(forms.ModelForm):

    class Meta:
        model = Customers
        fields = ['author', 'company', 'site', 'team', 'position', 'name', 'email', 'phone', 'note']

        widgets = {
            'company'  : forms.TextInput(attrs={'style':'width: 100%', 'placeholder': '(주)페르소나'}),
            'site'     : forms.TextInput(attrs={'style':'width: 100%', 'placeholder': 'Fab 이름'}),
            'team'     : forms.TextInput(attrs={'style':'width: 100%', 'placeholder': '그룹 or 팀명'}),
            'position' : forms.TextInput(attrs={'style':'width: 100%', 'placeholder': '직책명/직급명'}),
            'name'     : forms.TextInput(attrs={'style':'width: 100%', 'placeholder': '홍길동'}),
            'email'    : forms.TextInput(attrs={'style':'width: 100%', 'placeholder': 'emal@company.com'}),
            'phone'    : forms.TextInput(attrs={'style':'width: 100%', 'placeholder': '010XXXXXXXX(9자리)'}),
            'note'     : forms.Textarea(attrs={'style':'width: 100%', 'rows':1}),
        }

        labels = {
            'author'   : '작성자',
            'company'  : '회사명',
            'site'     : 'Site',
            'team'     : '소속',
            'position' : '직책/직위',
            'name'     : '이름',
            'email'    : 'Email',
            'phone'    : '전화번호',
            'note'     : '메모',
        }

 

sales/cutomer (고객관리) 만들기

 

- sales/urls : customer, customer_new, customer_new, customer_edit, customer_delete

from django.urls import path
from sales import views


app_name = 'sales'

urlpatterns = [
	path('customer/', views.customer, name='customer'),
	path('customer_view/<int:pk>', views.customer_view, name='customer_view'),
	path('customer_new/', views.customer_new, name='customer_new'),
	path('customer_view/<int:pk>/edit', views.customer_edit, name='customer_edit'),
	path('customer_view/<int:pk>/delete', views.customer_delete, name='customer_delete'),
]

 

sales/views : customer, customer_new, customer_new, customer_edit, customer_delete

from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from sales.models import Customers
from sales.forms import CustomerForm
from django.contrib import messages


@login_required
def customer(request):
	customers = Customers.objects.all().order_by('-customer_id')
	context = {'customers':customers}
	return render(request, 'customers/customer.html', context)

@login_required
def customer_view(request,pk):
	customers = Customers.objects.all().order_by('-customer_id')
	customer_view  = Customers.objects.get(pk=pk)
	context = {'customers':customers, 'customer_view':customer_view}
	return render(request, 'customers/customer_view.html', context)

@login_required
def customer_new(request):
	customers = Customers.objects.all().order_by('-customer_id')
	if request.method == 'POST':
		form = CustomerForm(request.POST)
		if form.is_valid():
			customer_new = Customers()
			customer_new.author   = form.cleaned_data['author']
			customer_new.company  = form.cleaned_data['company']
			customer_new.site     = form.cleaned_data['site']
			customer_new.team     = form.cleaned_data['team']
			customer_new.position = form.cleaned_data['position']
			customer_new.name     = form.cleaned_data['name']
			customer_new.email    = form.cleaned_data['email']
			customer_new.phone    = form.cleaned_data['phone']
			customer_new.note     = form.cleaned_data['note']
			customer_new.save()
			return redirect('sales:customer')
	else:
		form = CustomerForm()
		context = {'form':form, 'customers':customers}
		return render(request, 'customers/customer_new.html', context)

@login_required
def customer_delete(request, pk):
	customer_del = Customers.objects.get(pk=pk)
	if customer_del.author == User.objects.get(username = request.user.get_username()):
		customer_del.delete()
		customers = Customers.objects.all().order_by('-customer_id')
		return redirect('sales:customer')
	else:
		messages.info(request, "UserID가 일치하지 않습니다.")
		return render(request,'error_test.html')

@login_required
def customer_edit(request,pk):
	customers = Customers.objects.all().order_by('-customer_id')
	customer_edit = Customers.objects.get(pk=pk)
	if request.method == 'POST':
		form = CustomerForm(request.POST)
		if form.is_valid():
			customer_edit.author   = form.cleaned_data['author']
			customer_edit.company  = form.cleaned_data['company']
			customer_edit.site     = form.cleaned_data['site']
			customer_edit.team     = form.cleaned_data['team']
			customer_edit.position = form.cleaned_data['position']
			customer_edit.name     = form.cleaned_data['name']
			customer_edit.email    = form.cleaned_data['email']
			customer_edit.phone    = form.cleaned_data['phone']
			customer_edit.note     = form.cleaned_data['note']
			customer_edit.save()
			return redirect('sales:customer')
	else:
		form = CustomerForm(instance=customer_edit)
		context = {'form':form, 'customers':customers}
		return render(request, 'customers/customer_edit.html', context)

 

sales/templates/customers/ : customer, customer_new, customer_new, customer_edit

sales/templates/customers/customer.html

 

{% extends 'base/base.html' %}
{% block main %}
<div class="container-fluid px-4">
    <!-- Main_Header -->
    <h1 class="mt-4">영업</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a href="{% url 'sales:customer' %}">Customer</a></li>
    </ol>

    <!-- Main_content_#1 -->
    
    <!-- Main_content_#2 -->
    
    <!-- Main_content_#3 -->
    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-table me-1"></i>
            고객리스트 | <a href="{% url 'sales:customer_new' %}">글쓰기</a>
        </div>
        <div class="card-body">
            <table id="datatablesSimple">
                <thead>
                    <tr>
                        <th>순번</th>
                        <th>작성자</th>
                        <th>회사명</th>
                        <th>Site</th>
                        <th>소속</th>
                        <th>직책/직위</th>
                        <th>이름</th>
                        <th>Email</th>
                        <th>전화번호</th>
                        <th>메모</th>
                        <th>작성일</th>
                    </tr>
                </thead>
                <tbody>
                    {% for customer in customers %}
                    <tr>
                      <td>{{ customer.customer_id }}</td>
                      <td>{{ customer.author }}</td>
                      <td>{{ customer.company }}</td>
                      <td>{{ customer.site }}</td>
                      <td>{{ customer.team }}</td>
                      <td>{{ customer.position }}</td>
                      <td><a href="{% url 'sales:customer_view' customer.pk %}">{{ customer.name }}</a></td>
                      <td>{{ customer.email }}</td>
                      <td>{{ customer.phone }}</td>
                      <td>{{ customer.note }}</td>
                      <td>{{ customer.created_at | date:'Y-m-d'}}</td>
                    </tr>
                    {% endfor %}
                    
                </tbody>
            </table>
        </div>
    </div>
    <!-- Main_content_#4 -->
</div>
{% endblock main %}


{% block js %}
{% endblock js %}

 

sales/templates/customers/customer_edit.html

{% extends 'base/base.html' %}
{% block main %}
<div class="container-fluid px-4">
    <!-- Main_Header -->
    <h1 class="mt-4">영업</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a href="{% url 'sales:customer' %}">Customer</a></li>
        <li class="breadcrumb-item active">Customer_edit</li>
    </ol>

    <!-- Main_content_#1 -->
    
    <!-- Main_content_#2 -->
    
    <!-- Main_content_#3 -->
    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-table me-1"></i>
            고객관리 | <a href="{% url 'sales:customer_new' %}">글쓰기</a>
        </div>
        <div class="card-body">
            <table id="datatablesSimple">
                <thead>
                    <tr>
                        <th>순번</th>
                        <th>작성자</th>
                        <th>회사명</th>
                        <th>Site</th>
                        <th>소속</th>
                        <th>직책/직위</th>
                        <th>이름</th>
                        <th>Email</th>
                        <th>전화번호</th>
                        <th>메모</th>
                        <th>작성일</th>
                    </tr>
                </thead>
                <tbody>
                {% for customer in customers %}
                <tr>
                  <td>{{ customer.customer_id }}</td>
                  <td>{{ customer.author }}</td>
                  <td>{{ customer.company }}</td>
                  <td>{{ customer.site }}</td>
                  <td>{{ customer.team }}</td>
                  <td>{{ customer.position }}</td>
                  <td><a href="{% url 'sales:customer_view' customer.pk %}">{{ customer.name }}</a></td>
                  <td>{{ customer.email }}</td>
                  <td>{{ customer.phone }}</td>
                  <td>{{ customer.note }}</td>
                  <td>{{ customer.created_at | date:'Y-m-d'}}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <!-- Main_content_#4 -->
    <div class="card mb-4">
        <div class="card-header">
            <span style="font-size:0.9em"><a href="#">수정</a> | <a href="#">삭제</a></span>
            </ol> -->
            <span style="font-size:1.3em">수정하기</span>
        </div>
        <div class="card-body">
            <table id="datatablesSimple" class="dataTable-table">
                <thead>
                    <tr>
                        <th>작성자</th>
                        <th>회사명</th>
                        <th>Site</th>
                        <th>소속</th>
                        <th>직책/직위</th>
                        <th>이름</th>
                        <th>Email</th>
                        <th>전화번호</th>
                        <th>메모</th>
                    </tr>
                </thead>
                
                <tbody>
                    <tr>
                    <form method="POST">
                        {% csrf_token %}
                        {% for form_td in form %}
                        <td >{{ form_td }}</td>
                        {% endfor %}
                        <a href="{% url 'sales:customer' %}"><button type="submit">수정</button></a>
                    </form>  
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock main %}


{% block js %}
{% endblock js %}

 

sales/templates/customers/customer_new.html

{% extends 'base/base.html' %}
{% block main %}
<div class="container-fluid px-4">
    <!-- Main_Header -->
    <h1 class="mt-4">영업</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a href="{% url 'sales:customer' %}">Customer</a></li>
        <li class="breadcrumb-item active">Customer_new</li>
    </ol>

    <!-- Main_content_#1 -->
    
    <!-- Main_content_#2 -->
    
    <!-- Main_content_#3 -->
    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-table me-1"></i>
            고객관리 | <a href="{% url 'sales:customer_new' %}">글쓰기</a>
        </div>
        <div class="card-body">
            <table id="datatablesSimple">
                <thead>
                    <tr>
                        <th>순번</th>
                        <th>작성자</th>
                        <th>회사명</th>
                        <th>Site</th>
                        <th>소속</th>
                        <th>직책/직위</th>
                        <th>이름</th>
                        <th>Email</th>
                        <th>전화번호</th>
                        <th>메모</th>
                        <th>작성일</th>
                    </tr>
                </thead>
                <tbody>
                {% for customer in customers %}
                <tr>
                  <td>{{ customer.customer_id }}</td>
                  <td>{{ customer.author }}</td>
                  <td>{{ customer.company }}</td>
                  <td>{{ customer.site }}</td>
                  <td>{{ customer.team }}</td>
                  <td>{{ customer.position }}</td>
                  <td><a href="{% url 'sales:customer_view' customer.pk %}">{{ customer.name }}</a></td>
                  <td>{{ customer.email }}</td>
                  <td>{{ customer.phone }}</td>
                  <td>{{ customer.note }}</td>
                  <td>{{ customer.created_at | date:'Y-m-d'}}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <!-- Main_content_#4 -->
    <div class="card mb-4">
        <div class="card-header">
            <span style="font-size:1.3em">새글 작성하기</span>
        </div>
        <div class="card-body">
            <table id="datatablesSimple" class="dataTable-table">
                <thead>
                    <tr>
                        <th>작성자</th>
                        <th>회사명</th>
                        <th>Site</th>
                        <th>소속</th>
                        <th>직책/직위</th>
                        <th>이름</th>
                        <th>Email</th>
                        <th>전화번호</th>
                        <th>메모</th>
                    </tr>
                </thead>
                
                <tbody>
                    <tr>
                        <form method="POST">
                            {% csrf_token %}
                            {% for form_td in form %}
                            <td >{{ form_td }}</td>
                            {% endfor %}
                            <a href="{% url 'sales:customer' %}"><button type="submit">생성</button></a>
                        </form>        
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock main %}


{% block js %}
{% endblock js %}

 

sales/templates/customers/customer_view.html

{% extends 'base/base.html' %}
{% block main %}
<div class="container-fluid px-4">
    <!-- Main_Header -->
    <h1 class="mt-4">영업</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a href="{% url 'sales:customer' %}">Customer</a></li>
        <li class="breadcrumb-item active">Customer_view</li>
    </ol>

    <!-- Main_content_#1 -->
    
    <!-- Main_content_#2 -->
    
    <!-- Main_content_#3 -->
    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-table me-1"></i>
            고객관리 | <a href="{% url 'sales:customer_new' %}">글쓰기</a>
        </div>
        <div class="card-body">
            <table id="datatablesSimple">
                <thead>
                    <tr>
                        <th>순번</th>
                        <th>작성자</th>
                        <th>회사명</th>
                        <th>Site</th>
                        <th>소속</th>
                        <th>직책/직위</th>
                        <th>이름</th>
                        <th>Email</th>
                        <th>전화번호</th>
                        <th>메모</th>
                        <th>작성일</th>
                    </tr>
                </thead>
                <tbody>
                {% for customer in customers %}
                <tr>
                  <td>{{ customer.customer_id }}</td>
                  <td>{{ customer.author }}</td>
                  <td>{{ customer.company }}</td>
                  <td>{{ customer.site }}</td>
                  <td>{{ customer.team }}</td>
                  <td>{{ customer.position }}</td>
                  <td><a href="{% url 'sales:customer_view' customer.pk %}">{{ customer.name }}</a></td>
                  <td>{{ customer.email }}</td>
                  <td>{{ customer.phone }}</td>
                  <td>{{ customer.note }}</td>
                  <td>{{ customer.created_at | date:'Y-m-d'}}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <!-- Main_content_#4 -->
    <div class="card mb-4">
        <div class="card-header">
            <span style="font-size:0.9em"><a href="{% url 'sales:customer_edit' customer_view.pk %}">수정</a> | <a href="{% url 'sales:customer_delete' customer_view.pk %}">삭제</a></span>
        </div>
        <div class="card-body">
            <table id="datatablesSimple" class="dataTable-table">
                <thead>
                    <tr>
                        <th>작성자</th>
                        <th>회사명</th>
                        <th>Site</th>
                        <th>소속</th>
                        <th>직책/직위</th>
                        <th>이름</th>
                        <th>Email</th>
                        <th>전화번호</th>
                        <th>메모</th>
                    </tr>
                </thead>
                
                <tbody>
                    <tr>
                      <td>{{ customer_view.author }}</td>
                      <td>{{ customer_view.company }}</td>
                      <td>{{ customer_view.site }}</td>
                      <td>{{ customer_view.team }}</td>
                      <td>{{ customer_view.position }}</td>
                      <td>{{ customer_view.name }}</td>
                      <td>{{ customer_view.email }}</td>
                      <td>{{ customer_view.phone }}</td>
                      <td>{{ customer_view.note }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock main %}


{% block js %}
{% endblock js %}

 

templates/error_test.html

{% if messages %}
<div class="alert alert-light alert-dismissible fade show" role="alert">
    {% for message in messages %}
    <strong>{{ message.message }}</strong>
    {% endfor %}
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
{% endif %}

 

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함