Code wont let me pull data to modelform from database, :(

20 views
Skip to first unread message

Kean

unread,
Aug 16, 2019, 4:28:09 PM8/16/19
to Django users
Hi,

New to Django, and am really really struggling which something maybe others may find really easy..

I've created a custom database model, and the subsequent modelform template to enable creating a record and storing to database.
However I am struggling to create two instances, where the user, retrieves a specific record created by them and can edit or delete this via a template.

My setup is as follows:

I have a url. In the body I have called each of the record to display with the objects. I then added a button for edit and delete to each record.

My thought process was if the user presses the edit button, the user is directed to a URL which enables them to edit any of the existing objects details
specifically around that record, and the edit url recognises the PK, and takes the user to only edit that record with that PK.
Once in the modelform, they can see all the object related data, i.e name = John , Surname = Smith ... they can then change the data, and submit it 
to the database. The record is then stored with new data and same PK, into the databse and the user is then 
redirected  to the records overview page, to determine the next step, edit another record or delete.


-- This is my button setup for the edit -

template.

<div>
  <a href="{% /editbusiness %}" type="button" class="btn btn-primary btn-sm">Edit</button"></a>


--- This is url path to get to the edit record url ---


path('Business register/Business management/Admin/Edit or remove business/editbusiness', views.editbusiness, name='editbusiness'),

--- This is the view I have created to extract retrieve the post request and render it to the editing template, 


def editbusiness(request):
data = Businessownercreate.objects.all()
if request.method == "POST":
form = Businessownercreate(request.POST, instance=instance)

if form.is_valid():
form.save()
return redirect("accounts:editremovebusiness")
else:
form =  forms.Businessownercreate()
args = { "form": form }
return render(request, 'editbusiness.html', args)

--- This is the template that allows the user to edit the record via a modelform, based on the original Businessownercreate form, which just creates the nentity record.

{% load static %}


    <div class="container">
    <br>

        <form method="POST">
            {% csrf_token %}
                {{form.as_p}}

    <br>                
        <button class="btn" type = "submit">submit</button>    
    </form>

Sorry for the long mail, and the detail, but it's tough starting out with a project, and im jut trying to grow and learn.
Please can anyone provide any guidance on how I can resolve to pull the data to the edit modelform from the database and save the data, with final redirect.

Best,

K


RONAK JAIN

unread,
Aug 17, 2019, 12:13:14 AM8/17/19
to django...@googlegroups.com
Hey , 

I think ,  i am not sure but got it so here you need to develop url from the templates it's mean you are trying your url from tenplate page Right?


So ,  if you are connecting with them i think you need to correction on your url form...


Please let me first ,  i am getting right then,  i will try to solve your conditions.


Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/451152f3-3582-4e91-a2ba-3cf56e89329e%40googlegroups.com.

Kean

unread,
Aug 17, 2019, 12:19:43 PM8/17/19
to django...@googlegroups.com
HI Ronak,

 correct, i have an edit button in the template.html,  trying to get this butto to re-direct to 
a page where the form is presented with the editable data.

Best,

K
Reply all
Reply to author
Forward
0 new messages