0

I'm new to Django, and I am trying to show my DB on a web page being able to edit it.

This is my views.py

   def task1(request):
       if request.POST:
           form = AuthorForm(request.POST)
           if form.is_valid():
               form.save()
           return render(request,'tasks/index.html')
       else:
           form = AuthorForm()
       args = {}
       args.update(csrf(request))
       args['form'] = form
       return render_to_response('tasks/task1.html', {'authors':Author.objects.all()})

This way i can print my database on webpage

But i also want to be able to add new records to it

I used

return render_to_response('tasks/task1.html',args)

But how can i send both? As far as i understood there must be a dictionary.

Here's a template

{% extends "tasks/index.html" %}

{% load bootstrap3 %}
{% load staticfiles %}
{% bootstrap_css %}
{% bootstrap_javascript %}

{% bootstrap_messages %}


{% block tasks %}

<p>31231</p>
<form action="/task1/" method="post">{% csrf_token %}
    <ul>
        {{ form.as_table}}
    </ul>
    <input type="submit" value="Create" />
</form>

{% if authors.count > 0 %}
    {% for author in authors %}
    <ul>
        {{author.id}}  {{author.name}} {{author.body}}
    </ul>
    {{% endfor %}
{{% endif %}
{% endblock %}
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
Uasmi Nasser
  • 75
  • 1
  • 7

2 Answers2

1
args = {}
args.update(csrf(request))

args['form'] = form
args['authors'] = Author.objects.all()
return render_to_response('tasks/task1.html',args)
0
return render_to_response('tasks/task1.html', {'authors':Author.objects.all(),'foo':foo,'bar':bar})

or

args = {'authors':Author.objects.all(),'foo':foo,'bar':bar}
return render_to_response('tasks/task1.html',args)

note that you can simply use render instead of render_to_response from django >= 1.3

kylieCatt
  • 10,672
  • 5
  • 43
  • 51
taesu
  • 4,482
  • 4
  • 23
  • 41