简体   繁体   中英

I need to change the value of a field and redirect to a url on button click. How am I supposed to do it?

I am working on a problem where I need to change the value of a model field "verified" on button click and redirect it to mail url so the the verified users get mail. I am not familiar with ajax. Please help me out in doing this.

models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE, default=None, null=True)
    role = models.CharField(max_length=50, choices=Roles, default='client')
    verified =models.BooleanField(default = False,blank=True)

template:

 <td>

  < a class="btn btn-primary"><i class="feather icon-edit mr-1">Verify</i></a>
  <a class="btn btn-primary"><i class="feather icon-trash-2">Delete</a>
                                                        </td>

You will have to write a view that you can call in the HTML on the button click example :

from django.shortcuts import render    
from .models import UserProfile    
def verify_and_redirect(request) :
    prof = UserProfile()
    prof.verified = True 
    prof.save()
    return render(request,'app_name/male_tamplate.html', context=context)

Then you can map this view to a URL in your urls.py file as follows:

from django.urls import path
from . import views    
path ('verify',views.verify_and_redirect, name='verify')

Then in your html, you can associate the URL with the button as follows:

 < a class="btn btn-primary" href="{% url 'verify' %}"><i class="feather icon-edit mr-1">Verify</i></a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM