【问题标题】:How do I create a card view in HTML from the form data that I have made in django?如何根据我在 django 中制作的表单数据在 HTML 中创建卡片视图?
【发布时间】:2020-05-13 12:53:20
【问题描述】:

我正在创建一个 Django CRM,我在其中使用表单来获取有关任何公司的数据。我想做的是当我或任何人写下任何公司的详细信息并提交它们时——它们将被保存为 company.html 中的卡片视图。

有人可以帮我解决这个问题吗?

companies.html:

{% extends 'base.html' %}

{% load static %}

{% block content %}
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4 mt-4">
    <a href="{% url 'company-create' %}" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i class="fas fa-plus fa-sm text-white-50"></i> Create Company</a>
    </div>
    <div class="text-center">
        <a class="small" href="{% url 'dashboard' %}">Back</a>
    </div>


</div>
{% endblock content %}

公司创建.html:

{% extends 'base.html' %}

{% load crispy_forms_tags %} 

{% block content %}
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4 mt-4">
    <h1 class="h3 mb-0 text-gray-800">Company Create</h1>
    </div>          

    <!-- Main Content Here -->
    <div class="card o-hidden border-0 shadow-lg my-5">
      <div class="card-body p-0">
        <div class="row">
          <div class="col-lg-3"></div>
          <div class="col-lg-6">
            <div class="p-5">
              <div class="text-center">
                <h1 class="h4 text-gray-900 mb-4">Create a Company!</h1>
              </div>
              <form method="POST" action="" enctype="multipart/form-data">
                {% csrf_token %}
                {{user_form | crispy}}
                {{profile_form | crispy}}
                <button type="submit" class="btn btn-primary btn-block">Update</button>
              </form>
              <hr>
              <div class="text-center">
                <a class="small" href="{% url 'dashboard' %}">Back</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>



</div>
<!-- /.container-fluid -->
{% endblock content %}

forms.py:

from django import forms 
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

from apps.userprofile.models import Profile

class SignUpForm(UserCreationForm):

first_name = forms.CharField(max_length=30, required=False, help_text='Optional')
last_name = forms.CharField(max_length=30, required=False, help_text='Optional')
email = forms.EmailField(max_length=254, help_text='Enter a valid email address')

class Meta:
    model = User
    fields = [
        'username',
        'first_name',
        'last_name',
        'email',
        'password1',
        'password2',
    ]


class UserForm(forms.ModelForm):
class Meta:
    model = User
    fields = [
        'username',
        'first_name',
        'last_name',
        'email',
    ]


class ProfileForm(forms.ModelForm):
class Meta:
    model = Profile
    fields = [
        'bio',
        'phone_number',
        'birth_date',
        'profile_image'
    ]


class CompanyForm(forms.Form):
name = forms.CharField(label="Enter Company Name",max_length= 50)
email = forms.EmailField(label="Enter Email")
phone_number = forms.CharField(label="Enter Phone Number",max_length=12)
contact_name = forms.CharField(label="Enter Contact Persons Name",max_length=50)
file = forms.FileField()

class Meta:
    model = User
    fields = [
        'name',
        'email',
        'phone_number',
        'username'
        'profile_image',
    ]

views.py:

from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy

from django.views.generic import TemplateView, CreateView

from .forms import SignUpForm, UserForm, ProfileForm, CompanyForm
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.contrib import messages
from apps.userprofile.models import Profile

class HomeView(TemplateView):
template_name = 'common/home.html'

class DashboardView(LoginRequiredMixin, TemplateView):
template_name = 'common/dashboard.html'
login_url = reverse_lazy('home')

def get_context_data(self, **kwargs):
    # Call the base implementation first to get a context
    context = super().get_context_data(**kwargs)
    # Add in a QuerySet of all the books
    print(self.request.user.id)
    context['book_list'] = self.request.user
    return context

class SignUpView(CreateView):
form_class =  SignUpForm
success_url = reverse_lazy('home')
template_name = 'common/register.html'

class MessageView(TemplateView):
template_name = 'common/messages.html'

class CompanyView(TemplateView):
template_name = 'common/companies/companies.html'

class CompanyCreateView(LoginRequiredMixin, TemplateView):
user_form = CompanyForm
template_name = 'common/companies/company-create.html'

def post(self, request):

    post_data = request.POST or None
    file_data = request.FILES or None

    user_form = CompanyForm(post_data)

    if user_form.is_valid():
        user_form.save()
        messages.success(request, 'Your company was successfully created!')
        return HttpResponseRedirect(reverse_lazy('comapanies'))

    context = self.get_context_data(
                                    user_form=user_form
                                )

    return self.render_to_response(context)

def get(self, request, *args, **kwargs):
    return self.post(request, *args, **kwargs)

from django.http import HttpResponseRedirect
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.contrib import messages
from .forms import UserForm, ProfileForm
from django.contrib.auth.models import User
from apps.userprofile.models import Profile


class ProfileView(LoginRequiredMixin, TemplateView):
template_name = 'common/profile.html'

class ProfileUpdateView(LoginRequiredMixin, TemplateView):
user_form = UserForm
profile_form = ProfileForm
template_name = 'common/profile-update.html'

def post(self, request):

    post_data = request.POST or None
    file_data = request.FILES or None

    user_form = UserForm(post_data, instance=request.user)
    profile_form = ProfileForm(post_data, file_data, instance=request.user.profile)

    if user_form.is_valid() and profile_form.is_valid():
        user_form.save()
        profile_form.save()
        messages.success(request, 'Your profile was successfully updated!')
        return HttpResponseRedirect(reverse_lazy('profile'))

    context = self.get_context_data(
                                    user_form=user_form,
                                    profile_form=profile_form
                                )

    return self.render_to_response(context)

def get(self, request, *args, **kwargs):
    return self.post(request, *args, **kwargs)

除了代码,我还附上了一些 CRM 的屏幕截图:

仪表板: 就像称为公司和联系人的卡片一样,我希望将公司详细信息显示在卡片中 -

公司: 在单击创建联系人按钮后,我们将被重定向到公司创建表单。

创建公司: 现在,在使用填写的表单详细信息单击更新按钮后,我想将表单重定向到联系人页面,其中新添加的公司详细信息显示在卡片表单中的创建公司按钮下方。

【问题讨论】:

  • 所以,如果你使用 def 作为函数而不是 class 作为函数,对我来说很简单。在其中,您可以在其中调用模型,将其用作变量,放入函数字典并使用此参数的循环在 companies.html 中检索。
  • 你能举个例子说明你是如何处理它的吗??
  • 我添加了下面的示例,使用普通函数作为视图而不是类。试试这种方法,看看它是否可以帮助你。如果是,您可以将其添加为答案并加起来作为答案吗?我希望它对你有帮助。 :)

标签: python html django crm


【解决方案1】:

这是您请求使用普通函数作为视图而不是类的示例的方法。您需要在 settings.py 中进行一些设置,例如安装 Pillow(如果您还没有完成)。看看能不能帮到你。

models.py

from django.db import models


class CompanyDetail(models.Model):
name = models.CharField(label="Enter Company Name",max_length= 50)
email = models.EmailField(label="Enter Email")
phone_number = models.CharField(label="Enter Phone Number",max_length=12)
contact_name = models.CharField(label="Enter Contact Persons Name",max_length=50)

image = models.ImageField(upload_to='tour_images', blank=True)

views.py

from django.shortcuts import render,
from .models import CompanyDetail


def show_company_cards(request):

    company_details = CompanyDetail.objects.all()

    return render(request, 'company.html', {'company_details': company_details})

公司.html


{% extends 'base.html' %}

{% load static %}

{% block content %}
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4 mt-4">
    <a href="{% url 'company-create' %}" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i class="fas fa-plus fa-sm text-white-50"></i> Create Company</a>
    </div>
    <div class="text-center">
        <a class="small" href="{% url 'dashboard' %}">Back</a>
    </div>
<!-- this is the place of the closing container fluid that is in the bottom -->

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">
<div class="container">
    <div class="row">

    {% for detail in company_details %}

        <div class="col-md-3">
            <div class="contact-box center-version">


                    <img alt="image" class="img-circle" src="{{ detail.image.url}}">

                    <h3 class="m-b-xs"><strong>{{detail.name}}</strong></h3>

                    <div class="font-bold">{{detail.contact_name}}</div>

                    <address class="m-t-md">

                        <strong>{{detail.email}}</strong><br>

                        <abbr title="Phone">P:</abbr> {{detail.phone_number}}

                    </address>

            </div>
        </div>

{% endfor %}


     </div>
  </div>

<!-- container fluid closer (test to see if it works) if no remove an put it back-->

</div>
{% endblock content %}

【讨论】:

    猜你喜欢
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多