私の目標は、フォームを介してデータベースにデータを追加し、編集フォームで編集可能にすることです。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_edit.htmlするために、テンプレート/ car_list.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引数でフォームが作業をdoesntの。
の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 %}