【问题标题】:Python Django edit data in database by forms (django v.1.9.4)Python Django 按表单编辑数据库中的数据 (django v.1.9.4)
【发布时间】:2023-03-27 15:15:02
【问题描述】:

我的目标是通过表单将数据添加到数据库并通过编辑表单使它们可编辑。

我的错误:

异常类型:NoReverseMatch 位于 /testtable/car/new/
异常值:未找到带有参数“()”和关键字参数“{'pk': 2}”的“car_list”的反向。尝试了 0 个模式:[]

下一个问题是如何创建链接来编辑数据 在 template/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>

当我在 Explorer 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/template/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 %}

【问题讨论】:

    标签: forms python-3.x templates django-forms html-table


    【解决方案1】:

    我的意思是,car_list 不需要 pk 并且 django 找不到带有参数的 car_list 的任何 url。 也许您可以尝试替换行: 返回重定向('car_list',pk=car.pk) 和: 返回重定向('car_list')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 2018-09-06
      相关资源
      最近更新 更多