在Django中显示mysql数据库的数据

时间:2017-06-30 15:21:46

标签: python mysql django

我试图使用Django显示来自我的Mysql数据库的数据。用户通过addSubscriber.html页面输入所需数据以保存在数据库中,并重定向到页面' report.html'其中显示了表格中的所有数据(在本例中为订阅者)。

以下是我的文件:views.py

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.views import generic
from .models import Subscribers
from .forms import addSubsForm


@login_required
def dashboard(request):
    user = request.user
    context = {'user': user}
    template = 'dashboard.html'
    return render(request, template, context)



@login_required
def addSubscriber(request):
    template = 'addSubscriber.html'

    if request.method == 'POST':
            form = addSubsForm(request.POST)
            if form.is_valid():
                f = form.save(commit=False)
                f.save(using='db2')
                data = Subscribers.objects.all()
                return redirect('report', {'entries': data})

    else:
            form = addSubsForm()

    return render(request, template, {'form': form})




@login_required
def report(request):
    context = locals()
    template = 'report.html'
    return render(request, template, context)

forms.py

from .models import Subscribers
from django import forms


class addSubsForm(forms.ModelForm):
    class Meta:
        model = Subscribers
        fields = '__all__'

report.html

{%extends 'base.html'%}


{% block content %}

 <h2 class="page-header">Subscribers List</h2>


          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>Bill Number</th>
                  <th>Name</th>
                  <th>Area</th>
                  <th>Phone Number</th>
                  <th>Header</th>
                </tr>
              </thead>
              <tbody>
                <tr>{% for entries in Subscribers %}
                  <td>{{ entries.billNumber }}</td>
                  <td>{{ entries.name }}</td>
                  <td>{{ entries.area }}</td>
                  <td>{{ entries.phoneNumber }}</td>
                  <td>{{ entries.name }}</td>
                    {% endfor %}
                </tr>
              </tbody>
            </table>
          </div>
{% endblock %}

我提交表单时的错误是:

NoReverseMatch at /dashboard/addSubscriber/
Reverse for 'report' with arguments '({'entries': <QuerySet [(1, u'Manish Ojha', u'Jirikhimti', u'9843003979', u'2000', u'21'), (2, u'Manish Ojha', u'Jirikhimti', u'9843003979', u'2000', u'1'), (3, u'Manish Ojha', u'Jirikhimti', u'9843003979', u'2000', u'2'), (4, u'Manish Ojha', u'Jirikhimti', u'9843003979', u'2000', u'3'), (5, u'Manish Ojha', u'Jirikhimti', u'9843003979', u'2000', u'4'), (6, u'Manish Ojha', u'Jirikhimti', u'9843003979', u'2000', u'5'), (7, u'Manish Ojha', u'Jirikhimti', u'9843003979', u'2000', u'6'), (8, u'Manish Ojha', u'Jiri', u'8989', u'989', u'27308472893478'), (9, u'Manish Ojha', u'Jiri', u'8989', u'989', u'27308472893478'), (10, u'Manish Ojha', u'Jiri', u'8989', u'989', u'1'), (11, u'Manish Ojha', u'Jiri', u'8989', u'989', u'1'), (12, u'Manish', u'jklj', u'89890', u'99089', u'8989'), (13, u'Manish', u'jklj', u'89890', u'99089', u'8989'), (14, u'Manish', u'jklj', u'89890', u'99089', u'8989'), (15, u'Manish', u'jklj', u'89890', u'99089', u'8989'), (16, u'Manish', u'jklj', u'89890', u'99089', u'8989'), (17, u'Manish', u'jklj', u'89890', u'99089', u'8989'), (18, u'sdf', u'sdf', u'sdf', u'sdf', u'sdf'), (19, u'sdf', u'sdf', u'sdf', u'sdf', u'sdf'), (20, u'sfdf', u'jh', u'jhj', u'hj', u'hj'), '...(remaining elements truncated)...']>}, 1)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['dashboard/report/$']

即使这些数据是很久以前输入的,并且已经从数据库中删除,它们仍会显示在此处。

2 个答案:

答案 0 :(得分:3)

更改此行

return redirect('report', {'entries': data}, 1)

return redirect('report')

并在报告视图中

@login_required
def report(request):
    data = Subscribers.objects.all()
    template = 'report.html'
    return render(request, template, {'entries' : data})

并在html中更改此行

{% for entries in Subscribers %}

{% for entry in entries %}
<td>{{ entry .billNumber }}</td>

同样适用于其他tds

答案 1 :(得分:0)

  vm.downloadExcel = function() {
    this.jsonCustomer = JSON.stringify(vm.customer);
    $http.post('/downloadxlsx', this.jsonCustomer)
    .then(response => {
        let blob = new Blob([response], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
        let url = URL.createObjectURL(blob);
        let a = document.createElement('a');
        a.href = url;
        a.target = '_blank';
        a.click();
    })
  }

我认为这也做错了。尝试我的代码,看看我的是否有效

相关问题