2

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.

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

11 Comments

Sorry for the return redirect('report', {'entries': data}, 1). the 1 at the end was my mistake. I was experimenting with the code
The page is redirected to report.html but in report.html the datas from the database is not shown.
My god you are a genius. Thanks again bro
doing the same thing. this is my first app. But the documentation is a tough one. God its difficult to be a programmer. however trying my best
bro remember the start is always the hardest and to be frank bro even i dont know a lot of things of django :)
|
0
{% 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.