Home > Software engineering >  How to put data in the db when you press a button in django
How to put data in the db when you press a button in django

Time:03-18

<button  href="{% url 'one' one.id_n %}" >
<div >
<img  src="{{ one.f1 }}" alt="" />
</div>
</button >

I'm begginer with django and i want to add data as "one.id_n" to the db when you press the buttpm.

CodePudding user response:

You can do something like this :

Into urls.py:

urlpatterns = [
    path("one/<int:id_n>", views.get_one_view, name="one"),
    ...
]

Into models.py:

class MyOneModel(models.Model):
    one_id = models.IntField(default=-1)

Into views.py:

def get_one_view(request, id_n):
   
    MyOneModel.objects.create(one_id = id_n)        

    return render(request, "one.html", {})

CodePudding user response:

Django data submission is by http POST, which almost always corresponds to using a form and a view. Having POSTed, the view usually redirects to a new page. In other words, the "link" is the redirection in the view.

Sorry to say this, but you really need to work through one of the basic Django tutorials. I would advise skipping function-based views and learn to use Class-Based view FormView, but that's an opinion.

  • Related