数据库更新值django

时间:2017-07-12 09:20:58

标签: python mysql sql django

我是django的初学者,

所以我在models.py中创建了一个模型,并希望在任何人通过行的id点击该行时更新数据库。以下是我的所有files.edit_vendor()缺少1个必需的位置参数:' id'。如何获取行的id并在下一个更新页面上显示并更新数据库中的值

urls.py

from django.conf.urls import url,include
from . import views
from django.contrib.auth.views import login
urlpatterns = [
    url(r'^',views.get_contract, name='contract'),
    url(r'^update/', views.edit_vendor,name='update'),
    ]

views.py

from django.shortcuts import render
from .models import vendor,vendorForm
from django.http import HttpResponse,HttpResponseRedirect
from django.views.generic import UpdateView

# Create your views here.
def get_contract(request):
    # if this is a POST request we need to process the form data
    form_class = vendorForm
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form=form_class(request.POST)
        # check whether it's valid:
        if form.is_valid():
            party_name = form.cleaned_data['party_name']
            contact_person = form.cleaned_data['contact_person']
            phone_no = form.cleaned_data['phone_no']
            email = form.cleaned_data['email']
            material_description = form.cleaned_data['material_description']
            type_of_person = form.cleaned_data['type_of_person']
            type_of_dealer = form.cleaned_data['type_of_dealer']
            message = form.cleaned_data['message']
            form.save()
        else:
        # In reality we'd use a form class
        # to get proper validation errors.
            return HttpResponse('Make sure all fields are entered and valid.')

        return HttpResponseRedirect('contract')

    else:
        form = vendorForm()


    vendors = vendor.objects.all()
    return render(request,'vendors/contract.html',{'vendors': vendors,'form':form,})




def edit_vendor(request,id):
    if request.POST:
        vendor_form = vendorForm(request.POST)

        if vendor_form.is_valid():
            self.model,
            vendor = vendor.objects.get(id)
            vendor_form = vendorForm(request.POST, instance =vendor)
            vendor_form.save()
        else:
            vendor = vendor.objects.get(id)       
            vendor_form = vendorForm(instance=vendor)

            return render_to_response('vendors/update.html',{ 'form':form }, context_instance=RequestContext(request))

Models.py

from django.db import models
from django.forms import ModelForm
from django.core.urlresolvers import reverse_lazy

# Create your models here.
class vendor(models.Model):
    VENDOR = 'VR'
    CONTRACTOR = 'CO'
    type_of_choices = (
        (VENDOR, 'Vendor'),
        (CONTRACTOR, 'Contractor'),)

    MANUFACTURER = 'MR'
    DEALER = 'DR'
    type_of_making = (
        (MANUFACTURER, 'Manufacturer'),
        (DEALER, 'Dealer'),)    

    party_name = models.CharField(max_length=31)
    contact_person = models.CharField(max_length=100)
    phone_no =models.CharField(max_length=100)
    email =models.CharField(max_length=100)
    material_description =models.CharField(max_length=100)
    type_of_person= models.CharField(
        max_length=2,
        choices=type_of_choices,
        default=VENDOR,
    )
    type_of_dealer= models.CharField(
        max_length=2,
        choices=type_of_making,
        default=DEALER,
    )
    message =models.CharField(max_length=100)
    edithere =models.CharField(max_length=100)




class vendorForm(ModelForm):
    class Meta:
        model =vendor
        fields = ['party_name', 'contact_person', 'phone_no','email','material_description','type_of_person','type_of_dealer','message','id']

Update.html

!DOCTYPE html>
<html lang="en">





<head>

</head>




<body class="body">
        {% block content %}

<form method="POST">
    {% csrf_token %}
     {{ form.as_table }}
      <button type="submit" style="
    background-color: #4CAF50; /* Green */
    display: block;
    color: white;
    text-align: center;
    text-decoration: none;
    font-size: 16px;
">ADD</button>
</form>

        {% endblock %}
</body>

</html>

1 个答案:

答案 0 :(得分:0)

您需要在url edit_vendor视图中捕获关键字参数。尝试在urls.py

中添加类似的内容
url(r'^update/(?P<id>[\d]+)/$', views.edit_vendor,name='update'),

另外,在调用模板时,不要忘记将其传递到网址中

{% url 'update' row_item.id %}