Adding to database from another database using button in django
I am creating a web application in which there is a lot of data displayed in tables. I wanted to create a platform where there are various documents, and in order to see them, the user has to send a request for access. Meaning it browses what documents are available and asks for access to see their details. Now I've run into a problem that I can't solve.
I want to add data from one table to another without form. Only by clicking a button in a table row (this is the "request for access"). The data in that row would be added to another table.
Unfortunately, when I click the button, nothing happens. I know I'm probably going about this the wrong way, but when no problem is displayed, the page reloads normally, I don't have any ideas how to solve it.
I created models:
class CourtFile(models.Model):
court = models.ForeignKey(Court, on_delete=models.CASCADE, null=True)
signature = models.CharField(max_length=200, null=True)
article = models.CharField(max_length=200, null=True)
contents_list = models.FileField(upload_to = "filepath", null=True)
def __str__(self):
return f'{self.court} - {self.signature}'
class AccessRequest(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
status = models.BooleanField(null=True)
signature = models.ForeignKey(CourtFile, on_delete=models.CASCADE, null=True)
def __str__(self):
return f'{self.signature}'
I am displaying the table like this:
<tbody>
{% for courtfile in courtfiles_list %}
<tr>
<td>{{courtfile.signature}}</td>
<td>{{courtfile.court}}</td>
<td>{{courtfile.article}}</td>
<td><a href="{% url 'podglad' courtfile.id %}" button class="btn btn-primary" type="button">Podgląd</a></td>
<form method="POST">
{% csrf_token %}
<td><a href="{% url 'access' courtfile.id %}" button class="btn btn-primary" type="submit">Uzyskaj dostęp</a></td>
</form>
</tr>
{% endfor %}
</tbody>
The data is displayed on the "start_u" page, after pressing the button and adding data to another database, I would like to return to the "start_u" page And it's my views.py:
@login_required(login_url='login')
def startPage(request):
courtfiles_list = CourtFile.objects.all()
access_list = AccessRequest.objects.all()
context = {'courtfiles_list': courtfiles_list, 'access_list': access_list}
return render(request, 'users/start_u.html', context)
@login_required(login_url='login')
@allowed_users(allowed_roles=['user'])
def accessView(request, access_id):
access_request = CourtFile.objects.get(pk=access_id)
current_user = request.user
if request.method == 'POST':
entry = AccessRequest.objects.create(user=current_user, status=0, signature=access_request)
entry.save(force_insert=True)
return redirect('start_u')
urls.py:
path('start_u/', views.startPage, name="start_u"),
path('access/<access_id>', views.accessView, name="access"),