如何在Django中修复我的Createview

时间:2017-10-08 00:30:42

标签: python django

我正在尝试使用django python制作名片经理,但我不知道为什么我的名片没有添加。当我按下按钮" 添加名片"时,它会转到 BusinessCardListView ,但它是空白的。我还想知道如何使删除和更新按钮在名片列表上工作。我想我必须在模型中添加一个主键,但我不知道如何正确传递它。

浏览

from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.views.generic import View
from .models import BusinessInfo

class BusinessCardListView(generic.ListView):
    model = BusinessInfo
    template_name = 'manager/BusinessCardList.html'
    context_object_name = 'all_business_cards'

    def get_queryset(self):
        return BusinessInfo.objects.all()


class BusinessCardCreate(CreateView):
    model = BusinessInfo
    fields = ['card', 'company_name', 'phone_number', 'website', 'representative_name', 'branch_address', 'job_title',
              'fax_number', 'cell_phone_number', 'email']


class BusinessCardUpdate(UpdateView):
    model = BusinessInfo
    fields = ['card', 'company_name', 'phone_number', 'website', 'representative_name', 'branch_address', 'job_title',
              'fax_number', 'cell_phone_number', 'email']


class BusinessCardDelete(DeleteView):
    model = BusinessInfo
    success_url = reverse_lazy('manager:index')

添加名片表格

{% extends 'manager/base.html' %}
{% block title %}Add a New Business Card{% endblock %}
{% block albums_active %}active{% endblock %}
{% block body %}

<div class="container-fluid">
   <div class="row">
       <div class="col-sm-12 col-md-7">
            <div class="panel panel-default">
                <div class="panel-body">
                    <form class="form-horizontal" action="{% url 'manager:index' %}" method="post" enctype="multipart/form-data">
                        {% csrf_token %}
                        {% include 'manager/form_template.html' %}
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">Add Business Card</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

form_template

{% for field in form %}
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <span class="text-danger small">{{ field.error }}</span>
        </div>
        <label class="control-label col-sm-2">{{ field.label_tag }}</label>
        <div class="col-sm-10">{{ field }}</div>
    </div>
{% endfor %}

网址

from django.conf.urls import url
from . import views


app_name = 'manager'
urlpatterns = [
    url(r'^$', views.BusinessCardListView.as_view(), name='index'),
    url(r'business_card/add/$', views.BusinessCardCreate.as_view(), name='business_card-add'),
    url(r'business_card/(?P<pk>[0-9]+)/edit/$', views.BusinessCardUpdate.as_view(), name='edit'),
    url(r'business_card/(?P<pk>[0-9]+)/delete/$', views.BusinessCardDelete.as_view(), name='delete'),
]

模型

from django.db import models
from django.core.urlresolvers import reverse


# Business Card Info
class BusinessInfo(models.Model):
    card = models.FileField(default='Picture')
    company_name = models.CharField(max_length=100000, primary_key=True)
    website = models.CharField(max_length=100000)
    representative_name = models.CharField(max_length=100000)
    branch_address = models.CharField(max_length=100000)
    job_title = models.CharField(max_length=10000)
    email = models.EmailField()
    phone_number = models.CharField(max_length=100000)
    fax_number = models.CharField(max_length=100000)
    cell_phone_number = models.CharField(max_length=100000)

    def get_absolute_url(self):
        return reverse('manager:index')

    def __str__(self):
        return self.company_name + ':' + self.representative_name

名片列表

{% extends 'manager/Base.html' %}
{% block body %}
<style>
table, th, .Info {
    border: 1px solid black;
    border-collapse: collapse;
    text-align: center;
}
</style>


<table style="width:100%">
    <tr>
        <th>Business Card</th>
        <th>Company Name</th>
        <th>Representative Name</th>
        <th>Job Title</th>
        <th>Branch Address</th>
        <th>Website</th>
        <th>Phone Number</th>
        <th>Cell Phone Number</th>
        <th>Email</th>
        <th>Fax Number</th>
    </tr>
    {% for businessinfo in all_business_cards %}
        <tr>
            <td class="Info">{{ businessinfo.card }}</td>
            <td class="Info">{{ businessinfo.company_name }}</td>
            <td class="Info">{{ businessinfo.representative_name }}</td>
            <td class="Info">{{ businessinfo.job_title }}</td>
            <td class="Info">{{ businessinfo.branch_address }}</td>
            <td class="Info">{{ businessinfo.website }}</td>
            <td class="Info">{{ contactinfo.phone_number }}</td>
            <td class="Info">{{ contactinfo.cell_phone_number }}</td>
            <td class="Info">{{ contactinfo.email }}</td>
            <td class="Info">{{ contactinfo.fax_number }}</td>
            <td>
                <form action="{% url 'music:delete' %}" method="post" style="display: inline;">
                {% csrf_token %}
                    <input type="hidden" name="company_name" value="{{ company_name }}"/>
                    <button type="submit" class="btn btn-default btn-sm">
                        <span class="glyphicon glyphicon-trash"></span>
                    </button>
                </form>
            </td>
        </tr>
    {% endfor %}
</table>
{% endblock %}

1 个答案:

答案 0 :(得分:0)

名片制作表单模板中表单标记中的操作属性为 {%url'manager:index'%} 指向 BuisinessCardListView ,这就是为什么它会将您带到提交的列表视图。

要实现您想要的功能,应该指向CreateView网址,如下所示:

  {% extends 'manager/base.html' %}
  {% block title %}Add a New Business Card{% endblock %}
  {% block albums_active %}active{% endblock %}
  {% block body %}

 <div class="container-fluid">
 <div class="row">
    <div class="col-sm-12 col-md-7">
        <div class="panel panel-default">
            <div class="panel-body">
                <form class="form-horizontal" action="{% url 'manager:business_card-add' %}" method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    {% include 'manager/form_template.html' %}
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-success">Add Business Card</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
 </div>
 </div>
 {% endblock %}