【发布时间】: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中检索。 -
你能举个例子说明你是如何处理它的吗??
-
我添加了下面的示例,使用普通函数作为视图而不是类。试试这种方法,看看它是否可以帮助你。如果是,您可以将其添加为答案并加起来作为答案吗?我希望它对你有帮助。 :)