Django如何通过表单动作传递对象id?

时间:2017-09-05 03:08:08

标签: django django-forms

我仍然无法在表单的url中传递对象的id。

adjust.html

<form action="{% url 'classroom:deleteblock' classroom.id %}" method="post">
    {% csrf_token %}
    {{ form }}
<input type="submit" value="submit" />
</form>

models.py

class Classroom(models.Model):
    COURSE_NAME = (
        ('MA8', 'Math 8'),
        ('SC10', 'Science 10'),
        ('PH11', 'Physics 11'),
        ('PH12', 'Physics 12'),
    )
    BLOCK_NUMBER = (
        ('11', 'Block 1-1'),
        ('12', 'Block 1-2'),
        ('13', 'Block 1-3'),
        ('14', 'Block 1-4'),
        ('21', 'Block 2-1'),
        ('22', 'Block 2-2'),
        ('23', 'Block 2-3'),
        ('24', 'Block 2-4'),
    )
    class_list = models.TextField()
    course_name = models.CharField(max_length=20, choices=COURSE_NAME)
    course_block = models.CharField(max_length=10, choices=BLOCK_NUMBER)
    group_size = models.IntegerField(default=3)

    def __str__(self):
        return self.get_course_block_display()

    def save(self, *args, **kwargs):
        super(Classroom, self).save(*args, **kwargs)
        # overrides the default save function to parse the class list
        studentList = []
        studentList = self.class_list.split('\n')
        print (studentList)
        for line in studentList:
            line = line.strip('\r')
            s = Student.objects.create(nickname = line, classroom = self)

class DeleteForm(ModelForm):
    class Meta:
        model = Classroom
        fields = ['course_block']

views.py

class BlockDeleteView(DeleteView):
    model = Classroom

delete_block_view = BlockDeleteView.as_view()

def adjust(request):
    if request.method == 'POST':
        form = DeleteForm(request.POST)
        if form.is_valid():
            return render(request, 'classroom/adjust.html')
    else:
        form = DeleteForm()

    return render(request, 'classroom/adjust.html', {'form': form})

urls.py

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^submitted', views.submitted, name='submitted'),
    url(r'^classup/$', create_classroom_view, name='classroom'),
    url(r'^block/$', views.block, name='block'),
    url(r'^(?P<pk>[09-]+)/deleteblock/$', delete_block_view, name='deleteblock'),
    url(r'^adjust/$', views.adjust, name='adjust'),
    url(r'^random/$', list_random_view, name='random'),
]

所以我的想法是adjust.html为我提供了一个表单,其中包含与course_block相对应的一个选项下拉菜单。我选择课程,点击提交,希望我被重定向到classroom_form.html。表单的操作是将对象pk发送到DeleteView,我认为它应该删除该对象。

如上所述运行,我收到错误:Reverse for 'deleteblock' with arguments '('',)' not found. 1 pattern(s) tried: ['classroom/(?P<pk>[09-]+)/deleteblock/$']。如何正确传递操作网址以匹配deleteblock

我想我很快就会发现,但我的get_url_success()需要BlockDeleteView吗?

编辑:我也尝试在上下文中将查询传递给模板但是具有相同的错误。 views.py

def adjust(request):
    classroom = Classroom.objects.all().first()
    if request.method == 'POST':
        form = DeleteForm(request.POST)
        if form.is_valid():
            return render(request, 'classroom/adjust.html', {'form':form}, {'classroom':classroom})
    else:
        form = DeleteForm()

    return render(request, 'classroom/adjust.html', {'form': form}, {'classroom': classroom})

3 个答案:

答案 0 :(得分:2)

url(r'^(?P<pk>[0-9]+)/deleteblock/$', delete_block_view, name='deleteblock'),

[0-9]+而非[09-]+,并且您需要BlockDeleteView中的success_url或get_url_success()方法

更改您的views.py:

class BlockDeleteView(DeleteView):
    model = Classroom
    slug_field = 'id'
    success_url = reverse_lazy('some_url')
    template_name = 'classroom/delete.html'

urls.py

url(r'^(?P<pk>[0-9]+)/deleteblock/$', BlockDeleteView.as_view(), name='deleteblock'),

答案 1 :(得分:1)

我认为它应该是0-9而不是09-,在url_mapping的url_mapping的regex(正则表达式)部分用于url_patterns中的deleteblock。

试试这个:

url(r'^(?P<pk>[0-9]+)/deleteblock/$', delete_block_view, name='deleteblock')

而不是:

url(r'^(?P<pk>[09-]+)/deleteblock/$', delete_block_view, name='deleteblock')

答案 2 :(得分:0)

classroom.id不存在,您必须在上下文中传递该var,而您必须具体的网址应为{% url 'classroom:deleteblock' pk=classroom.id %}