0

urlpatterns=[
    path('login/',views.LoginUser,name='login'),
    path('logout/',views.LogoutUser,name='logout'),
    path('register/',views.RegisterUser,name='register'),
    path('delete/<str:pk>',views.DeleteUser,name='delete'),
    
    
    

    path('',views.home,name='home'),
    #path('usersSettings/',views.UserSettings,name='userSettings'),
    path('users/<str:pk>/',views.users,name='users'),
    path('parameters/',views.parameters,name='parameters'),
    path('EbotManual/',views.EbotManual,name='EbotManual'),
    path('LedManual/',views.LedManual,name='LedManual'),
    path('TestRutins/',views.TestRutins,name='TestRutins')
]

I am designing a website based on django. I want to update the user information and delete the user if wanted in the same page. I created updating and it works properly. But when I address the delete user function to same html file , the button that I want it to delete user also updates just like the other button. I need both buttons to work for their own purposes. I thought that without changing anything assigning delete function to button might help thats why I wrote the title like that. Thank you!

 <div class="login--wrapper">
        <form method="POST" class="form">
            {% csrf_token %}
            <div class="center">
                <h1>Kullanıcı Ayarları</h1>
                {% csrf_token %}
                {% for field in form  %}
                <div class="mb-3">
                    <label for="exampleInputPassword1" class="from-label">{{field.label}}</label>
                    {{field}}
                </div>
                {% endfor %}
                <button type="submit" class="btn btn-primary">Update Info</button>
                <button type="submit" class="btn btn-primary">Delete User </button>
                

            </div>
def DeleteUser(request,pk):
    user=DataEbotUser.objects.get(id=pk)
    if request.method=='POST':
        user.delete()
        
    
    context={'user':user}
    return render(request,'home/UsersPage.html',context)
def users(request,pk):  
    user=DataEbotUser.objects.get(id=pk)
    form=EditUserForm(instance=user)
    
    
    if request.method=='POST':
        form=EditUserForm(request.POST, instance=user)
        if form.is_valid():
            form.save()
            
            context={'form':form , 'users':users}
    return render(request,'home/UsersPage.html',context)
url patterns: 

urlpatterns=[
    path('login/',views.LoginUser,name='login'),
    path('logout/',views.LogoutUser,name='logout'),
    path('register/',views.RegisterUser,name='register'),
    path('delete/<str:pk>',views.DeleteUser,name='delete'),
    
    
    

    path('',views.home,name='home'),
    #path('usersSettings/',views.UserSettings,name='userSettings'),
    path('users/<str:pk>/',views.users,name='users'),
    path('parameters/',views.parameters,name='parameters'),
    path('EbotManual/',views.EbotManual,name='EbotManual'),
    path('LedManual/',views.LedManual,name='LedManual'),
    path('TestRutins/',views.TestRutins,name='TestRutins')
]
emrec
  • 1
  • 4
  • If you want these two actions to happen on the same page, why are you rendering two different pages for each of these actions ? `UsersPage.html` and `DeleteUser.html` – Tobin Aug 17 '22 at 19:26
  • Oh I was try something different , forget to change. Thanks I will edit now. The problem still persists though. – emrec Aug 17 '22 at 19:32
  • Here in this stackoverflow question/answer could be your solution, [Link to stackoverflow](https://stackoverflow.com/questions/35666570/django-form-with-two-submit-buttons-one-requires-fields-and-one-doesnt) – gomez_ Aug 18 '22 at 12:02

1 Answers1

0

The problem is that your two buttons submit the form to the same page that rendered the form. There is no way to tell them apart.

If you want each button to perform a different action, one way to do this would be:

<button type="submit" class="btn btn-primary">Update Info</button>
<a class="btn btn-danger" type="button" href="{% url 'delete' user.pk %}">Delete User</a>

Since the function users is the only one to manage the update of the user's information based on the form data, the button Update Info remains in the form of submit button.

The Delete User button on the other hand is different. It simply calls the function DeleteUser passing it a pk which will be used to delete a user.

Here are some things to consider:

  • The function DeleteUser must not be called directly. It is the function users which must render the page.
  • You have to render the user object in the context of your function users, to be able to retrieve the pk of the user who will be used for the button delete
  • Function DeleteUser must not render the template but redirect to another url like home. Something like return redirect('home')
Tobin
  • 2,029
  • 2
  • 11
  • 19
  • I am having a page not found error. I guess there is something wrong with the urlpatterns because my users path is dynamic with when I make formaction /users it cant find a page. I think if there is an different usage of formaction that may work. I added the url patterns to question can you please check . – emrec Aug 17 '22 at 20:09
  • First of all thank you so much for your support , I did few changes such as redirecting and changing Html file and I deleted the if statement in DeleteUser function now it works but when I delete the user , because the user is deleted having an noreversematch error and now Im working on it. – emrec Aug 17 '22 at 21:54
  • Actually you are right. It would be necessary to redirect to another url, such as `home`. – Tobin Aug 17 '22 at 22:25