0
I am making a simple CRUD, using 2 tables, the default user of Django and the citizen I created, I am using a foreinkey username to link the two, but in the form, instead of appearing a username (that is the logged-in user), appears of all registered users.
code models.py
from django.db import models
from django.contrib.auth.models import User
class Cidadao(models.Model):
nomecompleto = models.CharField(max_length=100)
endereco = models.CharField(max_length=100)
email = models.EmailField()
nomebens = models.CharField(max_length=100)
numSerie = models.CharField(max_length=100)
observacao = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return self.user
code views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from cidadania3.forms import CidadaoForm
from cidadania3.models import Cidadao
# Create your views here.
def home(request):
data = {}
search = request.GET.get('search')
if search:
data['db'] = Cidadao.objects.filter(email__icontains=search, user=request.user)
else:
data['db'] = Cidadao.objects.all().filter(user=request.user)
return render(request, 'index.html', data)
@login_required
def form(request):
Cidadao.objects.all().filter(user=request.user)
data = {}
data['form'] = CidadaoForm()
return render(request, 'form.html', data)
def create(request):
form = CidadaoForm(request.POST)
if form.is_valid():
form.save()
return redirect('home')
@login_required
def view(request, pk):
data = {}
data['db'] = Cidadao.objects.get(pk=pk)
return render(request, 'view.html', data)
@login_required
def edit(request, pk):
data = {}
data['db'] = Cidadao.objects.get(pk=pk)
data['form'] = CidadaoForm(instance=data['db'])
return render(request, 'form.html', data)
@login_required
def update(request, pk):
data = {}
data['db'] = Cidadao.objects.get(pk=pk)
form = CidadaoForm(request.POST or None, instance=data['db'])
if form.is_valid():
form.save()
return redirect('home')
@login_required
def delete(request, pk):
db = Cidadao.objects.get(pk=pk)
db.delete()
return redirect('home')
Forms.py code
from django.forms import ModelForm
from cidadania3.models import Cidadao
# Create the form class.
class CidadaoForm(ModelForm):
class Meta:
model = Cidadao
fields = ['nomecompleto', 'endereco', 'email', 'nomebens', 'numSerie', 'observacao', 'user']
In the user part was to be only the registered user (which is the name set in the button above ) Thanks in advance.
Code Urls.py
from django.contrib import admin
from django.urls import path, include
from cidadania3.views import home, form, create, view, edit, update, delete
urlpatterns = [
path('admin/', admin.site.urls),
path('', home, name='home'),
path('form/', form, name='form'),
path('create/', create, name='create'),
path('view/<int:pk>/', view, name='view'),
path('edit/<int:pk>/', edit, name='edit'),
path('update/<int:pk>/', update, name='update'),
path('delete/<int:pk>/', delete, name='delete'),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls')),
]
You could update your post with.py urls as well?
– Paulo Marques