2016-04-22 72 views
0

我的目標是通過窗體將數據添加到數據庫,並通過編輯窗體使它們可編輯。Python Django按窗體編輯數據庫中的數據(django v.1.9.4)

我的錯誤:

Exception Type: NoReverseMatch at /testtable/car/new/
Exception Value: Reverse for 'car_list' with arguments '()' and keyword arguments '{'pk': 2}' not found. 0 pattern(s) tried: []

和接下來的問題是如何讓一個鏈接編輯模板/ car_list.html數據 到car_edit.html。

<td><a href=>Edit</a> <a href="">Delete</a></td> 
<td><a href="{% url 'tabletest:car_edit' pk=car.pk %}">Edit</a> <a href="">Delete</a></td> 

當我手動在資源管理器http://localhost:8000/testtable/car/1/它的作品類型,我看到在數據庫第一篇文章的細節,但PK參數的形式不工作。

MyApp的/ TestTable的/ models.py

from django.db import models 
import datetime 
from django.db import models 
from django.utils import timezone 
from django.contrib.auth.models import User 

class TableParemeters(models.Model): 
    author = models.ForeignKey('auth.User') 
    car_brand = models.CharField(max_length=20) 
    car_type = models.CharField(max_length=20) 
    car_colour = models.CharField(max_length=20) 
    car_fuel = models.CharField(max_length=20) 
    car_trans = models.CharField(max_length=20) 
    car_license_plate = models.CharField(max_length=20) 
    created_date = models.DateTimeField(default=timezone.now) 

def __str__(self): 
    return "%s %s" % (self.car_brand, self.car_type) 

MyApp的/ TestTable的/ views.py

from django.shortcuts import redirect, get_object_or_404, render_to_response, render 
from django.http import HttpResponseRedirect, HttpResponse 
from django.core.urlresolvers import reverse 
from django.template import loader 
from django.views import generic 
from django.utils import timezone 
from django.template import RequestContext 
from django.core.context_processors import csrf 
from django.contrib.auth.models import User 

from .models import TableParemeters 
from .forms import CarForm 

def car_list(request): 
    cars = TableParemeters.objects.all().order_by('id') 
    return render(request, 'testtable/car_list.html', {'cars': cars}) 

def car_detail(request, pk): 
    car = get_object_or_404(TableParemeters, pk=pk) 
    return render(request, 'testtable/car_detail.html', {'car': car}) 

def car_new(request): 
    if request.method == "POST": 
     form = CarForm(request.POST) 
     if form.is_valid(): 
      car = form.save(commit=False) 
      car.save() 
      return redirect ('car_list', pk=car.pk) 
    else: 
     form = CarForm() 
    return render(request, 'testtable/car_new.html', {'form': form})    

def car_edit(request, pk): 
    car = get_object_or_404(TableParemeters, pk=pk) 
    if request.method == "POST": 
     form = CarForm(request.POST, instance=car) 
     if form.is_valid(): 
      car = formsave(commit=False) 
      car.save() 
      return redirect('car_detail', pk=car.pk) 
    else: 
     form = CarForm(instance=car) 
    return render(request, 'testtable/car_edit.html', {'form': form})   

MyApp的/ TestTable的/ forms.py

from django import forms 
from .models import TableParemeters 

class CarForm(forms.ModelForm): 

    class Meta: 

     model = TableParemeters 
     fields = '__all__' 

MyApp的/ TestTable的/ urls.py

from django.conf.urls import include, url 
from . import views 

app_name = 'testtable' 

urlpatterns = [ 
    #login 
    #url(r'^$', views.login_page, name='login'), 
    #logout 
    #carlist url 
    url(r'^$', views.car_list, name='car_list'), 
    #detail car url 
    url(r'^car/(?P<pk>\d+)/$', views.car_detail, name='car_detail'), 
    #add new car to list url 
    url(r'^car/new/$', views.car_new, name='car_new'), 
    #edit car in the list 
    url(r'^car/(?P<pk>\d+)/edit/$', views.car_edit, name='car_edit'), 
    ] 

MyApp的/ TestTable的/模板/ TestTable的/ car_list.html

{% extends 'testtable/base.html' %} 
{% block content %} 
<table class="table table-striped"> 
    <tbody> 
     <tr> 
     <th>ID</th> 
     <th>Brand</th> 
     <th>Type</th> 
     <th>Colour</th> 
     <th>Fuel</th> 
     <th>Transmition</th> 
     <th>License Plate</th> 
     <th>Created Date</th> 
     <th>Author</th> 
     <th></th> 
    </tr>   
{% for testtable in cars %} 
    <tr> 
     <td>{{ testtable.car_id }}</a></td> 
     <td>{{ testtable.car_brand }}</a></td> 
     <td>{{ testtable.car_type }}</td> 
     <td>{{ testtable.car_colour }}</td> 
     <td>{{ testtable.car_fuel }}</td> 
     <td>{{ testtable.car_trans }}</td> 
     <td>{{ testtable.car_license_plate }}</td> 
     <td>{{ testtable.created_date }}</td> 
     <td>{{ testtable.author }}</td> 
     <td><a href=>Edit</a> <a href="">Delete</a></td> 
    </tr>  
{% endfor %} 
</tbody> 
</table> 
{% endblock %} 

回答

0

我的意思是,car_list不需要pk和Django中無法找到與參數car_list任何URL。 ('car_list',pk = car.pk) 用: return redirect('car_list')