0

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'),

2 Answers 2

1

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.

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

Comments

0

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.

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.