简体   繁体   中英

Display data form mysql database in Django

I have tried to display data from my Mysql database using Django. The user enters the required data through addSubscriber.html page to save in the database and is redirected to a page 'report.html' where all the data from the table (in this case subscribers) is displayed.

Here are my files: 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 %}

The error when ever I submit the form is:

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/$']

Even these datas were entered a long time ago and already erased from the database they are still displayed here.

change this line

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

to

return redirect('report')

and in report view

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

and change this line in the html

{% for entries in Subscribers %}

to

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

same for other tds also

{% for entries in Subscribers %} 
<tr>
    <td>{{ entries.billNumber }}</td>
    <td>{{ entries.name }}</td>
    <td>{{ entries.area }}</td>
    <td>{{ entries.phoneNumber }}</td>
    <td>{{ entries.name }}</td>
</tr>
{% endfor %}

I think this was also done wrong. Try my code and see if mine works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM