pfn
pfn

Reputation: 97

Linking button to new page in Django not working

I have created an update view and am trying to link it to my form but the button linking doesn't seem to be working at all

urls.py

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

template:

{% 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/')

I added the relevant code, I feel like its a small error but I can't seem to identify it since I'm very new to Django, any help would be appreciated!

Upvotes: 0

Views: 1317

Answers (2)

ilkek
ilkek

Reputation: 264

You should refer to your app to use that URL because Django doesn't know to which app your URL name belongs to by default. So it'd be something like <button href="{% url 'appname:uploadupdate' uploads.upload_id %}" type="submit" class="edit" formmethod="POST">Edit</button>. Of course make sure that your project's URL Configuration includes your app's URLs. Hope it helps.

UPDATE

From the Django Documentation:

...
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)

Notes:

  • FormView inherits TemplateResponseMixin so template_name can be used here.

  • The default implementation for form_valid() simply redirects to the success_url.

So you should edit your View.

Upvotes: 2

khebila zaurt
khebila zaurt

Reputation: 1

dont do this in your forms , Use it for getting the Fields from the models and the view allows you to connect your forms directly with your page you can directly print our data in a page so there is an example :

My models i have Question and ansewer attribute :

------ 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>

for more information https://youtu.be/uz5gyXemak0

Upvotes: 0

Related Questions