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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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


--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/451152f3-3582-4e91-a2ba-3cf56e89329e%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

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

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

On Sat, 17 Aug 2019, 01:58 Kean, <[hidden email]> wrote:
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


--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/451152f3-3582-4e91-a2ba-3cf56e89329e%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BAqMUcM28MvuX660HCrtZ07gbFhS7dP_j2iKAjXEBhbuRWs2w%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

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

Kean Dumba
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
On 17 Aug 2019, at 05:12, RONAK JAIN <[hidden email]> wrote:

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

On Sat, 17 Aug 2019, 01:58 Kean, <[hidden email]> wrote:
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



--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/451152f3-3582-4e91-a2ba-3cf56e89329e%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BAqMUcM28MvuX660HCrtZ07gbFhS7dP_j2iKAjXEBhbuRWs2w%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/482A6470-857A-45F6-8683-072B84EBD4E7%40gmail.com.