PrinterPaper
PrinterPaper

Reputation: 11

django the way to access data from input form

My symptom is when I click the modify button and then I write down the information on new window that is implemented by bootstrap div part. However, my database doesn't change at all. Please ignore ... in codes, I delete attributes that looks messy. Codes can have typo, because I wrote it down manually to find a bug, but I didn't find.

I tried in view.py, address_modify makes return Httpresponse(street), but It returned None.

view.py

def address_modify(request, adid):
cat = get_object_or_404(Address, adid=adid)
if request.method == "POST":
    old_adid = adid
    email = request.user.email
    street = request.POST.get("street", None)
    city = request.POST.get("city", None)
...
    Address.objects.filter(adid=adid).update(..., street=street, city=city, state=state, ...)
    return redirect('/address/')
return redirect('/address/')

template ( I name it address.html)

<button class="btn btn-success" data-toggle="modal" data-target="#modify">MODIFY</button>
<div class ="model fade" id="modify" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<from action="" method="POST">{% csrf_token %}
</div>
<div class="modal-body">
<input type="text" name="street">
<input type="text" name="city">
...
...
<input type="text" name="zipcode">
</div>
<div class="modal-footer">
<a href="{% url 'address_modify' i.adid %}">{% csrf_token %}
<button type="button" class="btn btn-primary">Save Change</button></a>
<div></form>

urls.py

 url(r'^address_modify/(?P<adid>[0-9]+)/$', MyAppView.address_modify, name='address_modify'),

Upvotes: 0

Views: 52

Answers (2)

Borut
Borut

Reputation: 3364

That is not how you implement form and form submit. Your link is not submitting anything, it's just opening a link. This is the standard form syntax:

<form method="POST">
  {% csrf_token %}
  ... your form input fields here ...
  <input type="submit" value="Save changes">
</form>

You must submit the form. Note type="submit" there.

Next to that, Django has forms feature. Use it. Create forms.py as @Saumel-Omole suggested. Form for model Address would look like this:

class AddressForm(forms.ModelForm):
    class Meta:
        model = Address
        fields = '__all__'

Then you modify your view to use the form like:

 def address_modify(request, adid):

     cat = get_object_or_404(Address, adid=adid)
     form = AddressForm(instance=cat)
     if request.method == 'POST':
         form = AddressForm(request.POST, instance=cat)
         if form.is_valid():
             form.save()
             return redirect('/address/')
         else:
             print(form.errors) # change to logging

      return render(request, 'address.html', {'form': form})

Go over the official Django tutorial. These basics are all there. Maybe it is going to take you a day or two to get through it, but long-term that's going to be far less than guessing and googling around for days for basic stuff.

Upvotes: 0

Samuel Omole
Samuel Omole

Reputation: 185

In django the best practice is to create a forms.py file to handle forms, its really easy you can read the doumentation on it, basically the form will ensure that all your data are read.

Upvotes: 1

Related Questions