将按钮链接到Django中的新页面不起作用

时间:2019-04-11 20:06:46

标签: html django django-forms

我创建了一个更新视图,并试图将其链接到我的表单 但按钮链接似乎根本不起作用

urls.py

path('uploadupdate/<int:upload_id>', UploadUpdate.as_view(), name='uploadupdate'),

模板:

{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>Manage uploads</title>
<link rel="stylesheet" href="{% static 'studyadmin/manageuploads_css.css' %}">
</head>

<body>
<table >
<tr class="main_row"> 
<th> Upload Name </th>
<th> Author Name </th>
<th> Type </th>
<th> Date </th>
<th> Edit </th>
<th>Edit Tags</th>

</tr>
<form >
{% csrf_token %}
{% for uploads in uploads %} <!-- shows all contents -->
<tr>
    <th> {{ uploads.title}} </th>
    <th> {{ uploads.author }} </th>
    <th> {{ uploads.upload_type }} </th>
    <th> {{ uploads.date_posted|date:"d M, Y"  }} </th>

    <th> <button href="{% url 'uploadupdate' uploads.upload_id  %}" type="submit" class="edit" formmethod="POST">Edit</button>  </th>
    <th> <button href="#" type="submit" class="edittags" formmethod="POST" >Edit tags</button>  </th>

{% endfor %}
</form>
</table>
</body>
</html>

views.py

class UploadUpdate(UpdateView):
form_class = UploadEditForm
template_name = 'studyadmin/upload_update_form.html'
queryset = Uploads.objects.all()

def get_object(self, queryset=None):
    obj = Uploads.objects.get(upload_id=self.kwargs['upload_id'])
    print(obj)
    return obj

def form_valid(self, form):
    upload = form.save(commit=False)
    upload.save()
    return redirect('/manageupload/')

我添加了相关的代码,我觉得它是一个小错误,但是由于我是Django的新手,所以我似乎无法识别它,

2 个答案:

答案 0 :(得分:1)

您应该引用您的应用程序以使用该URL,因为Django不知道您的URL名称默认属于哪个应用程序。因此它类似于//@ts-ignore。当然,请确保您项目的<button href="{% url 'appname:uploadupdate' uploads.upload_id %}" type="submit" class="edit" formmethod="POST">Edit</button>包含应用程序的URL。希望对您有所帮助。

更新

来自Django文档:

URL Configuration

注释:

  • FormView继承了TemplateResponseMixin,因此可以在此处使用template_name。

  • ... class ContactView(FormView): template_name = 'contact.html' form_class = ContactForm success_url = '/thanks/' def form_valid(self, form): form.send_email() return super().form_valid(form) 的默认实现只是重定向到form_valid()

因此,您应该编辑视图。

答案 1 :(得分:0)

不要在表单中执行此操作,将其用于从模型中获取字段,并且视图允许您将表单直接与页面连接,可以在页面中直接打印我们的数据,因此有一个示例:

我的模型有Question和ansewer属性:

------ models.py ------

class Question_cours(models.Model):
    quest = models.CharField(max_length= 200 ) ---> question 
    rep = models.CharField(max_length = 20)  ----> answer 

--------- forms.py --------
class Form_question(forms.Form): /// important to make the same name fields 
        quest = forms.CharField()
        rep = forms.CharField()



 -------- view.py ----------


#-----------------------------------------------------------------------------
def add_question_cours(request):
    form = Form_question()
    if request.method == "POST":
        form = Form_question(request.POST)

    if form.is_valid() :
        Question_cours.objects.create(**form.cleaned_data)
    else : 
        print(form.errors)

    return render(request , 'bootstrap/add_question.html',context)
#------------------------------------------------------------------------------

----------- urls.py ------------

  path('dfsdf',views.add_question_cours,name='add_question')

----------------- add_question.html ----------

<form method="POST">{% csrf_token %}
<div class ="form-group">
{{ form.as_p}}
</div>

<button type =" submit" class="btn btn-danger ">
<i class ="fa fa-plus"></i>
</button>

了解更多信息 https://youtu.be/uz5gyXemak0