I am using custom user authentication for verifying user. I have table named voter and I am tacking username and password via from and matching that with username and password of table if it match user will be logged in and allowed them to pass to next page which contain form. In that form I initiated data but it will not appear automatically it will appear only when i refresh the page
code of my files is bellow (for some big file only relevant code is included that's why it is partial)
model.py (partial)
class Voter(models.Model):
serial_voter_id = models.AutoField(primary_key=True)
voter_id = models.CharField(unique=True, max_length=10)
voter_name = models.CharField(max_length=255)
voter_constituency = models.ForeignKey(Constituency, models.DO_NOTHING, blank=True, null=True)
username = models.CharField(unique=True, max_length=32)
password = models.TextField()
voter_address = models.CharField(max_length=255, blank=True, null=True)
area = models.CharField(max_length=10, blank=True, null=True)
city = models.CharField(max_length=10, blank=True, null=True)
pincode = models.IntegerField(blank=True, null=True)
adhar_no = models.BigIntegerField(unique=True)
birth_date = models.DateField()
age = models.IntegerField()
fingerprint = models.TextField(blank=True, null=True)
authenticity = models.CharField(max_length=3, blank=True, null=True)
wallet_id = models.TextField()
class Meta:
managed = False
db_table = 'voter'
forms.py
from django import forms
from .models import Voter
class VoterForm(forms.ModelForm):
class Meta:
model = Voter
fields = [
'serial_voter_id',
'voter_id',
'voter_name',
'voter_constituency',
'username',
'voter_address',
'area',
'city',
'pincode',
'adhar_no',
'birth_date',
'age',
'authenticity',
'wallet_id'
]
views.py (partial)
from .models import Voter
from .forms import VoterForm
def voter_login(request, *args, **kwargs):
contex = {}
return render(request, "poll/voter_login.html", contex)
def voter_verification(request, *args, **kwargs):
f_username = request.POST.get('username')
voter = Voter.objects.get(voter_id=1) # thing need to be dynamic hear by replacing it with username
f_username = request.POST.get('username')
f_password = request.POST.get('password')
u_password = voter.password # fetching the password from voter object
u_password = u_password.decode() # decoding binary password to string
form = VoterForm(request.POST or None, instance=voter)
if form.is_valid():
form.save()
form = VoterForm()
contex = {
'voter' : voter,
'f_username' : f_username,
'f_password' : f_password,
'u_password' : u_password,
'form' : form
}
# compare hear username and password entered by user and from database if these are correcgt then allow this view or redirect to voter_login view
if voter.username == f_username and u_password == f_password:
return render(request, "poll/voter_verification.html", contex)
else:
return render(request, "poll/voter_login.html", {})
voter_login.html
{% extends 'header.html' %}
{% block content %}
<table>
<form method="get" action='voter_verification'> {% csrf_token %}
username <input type="text" name="username">
password <input type="password" name="password">
<input type="submit" name="login" value="login">
</form>
{% endblock %}
voter_verification.html (template file)
<!DOCTYPE html>
<html>
<body>
{% if f_username == voter.username and f_password == u_password %}
<h1>verified</h1>
{% else %}
<h1>wrong id and password</h1>
{% endif %}
<form method='post' action='vote_page'> {% csrf_token %}
{{ form.as_p }}
<input type='submit' value="sumbit">
</form>
</body>
</html>
changes query in view model ( def voter_verification(request, *args, **kwargs):)
def voter_verification(request, *args, **kwargs):
f_username = request.POST.get('username')
voter = Voter.objects.get(username=f_username)
then request user check in voter table which have exits with same user name.
Thanks to DrMaxwellEdison from reddit for providing answer
https://www.reddit.com/r/djangolearning/comments/fecn1f/question_django_data_in_form_only_appear_after/
Please do not have a separate model that stores usernames and passwords aside from your User model. You can simply add a OneToOneField to the User model to connect this Voter model to the authenticated User, and you don't need to do any extra data storage (plus, you are more likely to be handling the password information incorrectly, exposing it to being compromised if your site is breached).
Also per your comment on SO, the POST is not working because of CSRF protection (the traceback of your error would likely have indicated this already). Please see those docs for details on how to proceed (hint: do not disable CSRF!).
Related
I am building a django project that allows users to singup and then post projects.
Apps include a 'users' app and a 'projects' app.
I am using #signals so that 'users' create/edit a Profile and the #signals ensure that the underlying django User model is always in synch with the Profile model contained in the 'users' app.
I have one Profile for the superuser and this is set up to not only access its own 'user profile' and 'project posts' that it may post, but is also set up to review the projects posted by other users before they can be seen in the public feed of 'project posts'.
In the Project model (within the projects app models.py) i have a field called 'is_approved' with a default value of false and 'project posts' are only visible in the public feed after the superadmin has used its template view of a model form to update 'is_approved' from False to True.
Each 'project post' includes a Profile field as a foreign key (one-to-many)
The above is working very well - as the superuser can be the sessionid authenticated user and yet update the 'project posts' that belong to a different profile.
My problem now is trying to do the above when logged in as the superuser, but attempting to update fields in other Profiles (such as 'is_accepted' from False to True) through a model Form.
I need to do this so that when a Profile is edited it can have its 'is_accepted' status returned to False, so that the superuser can then review before the Profile returns to the public list of Profiles.
With the code below, I first check that it is the superuser attempting to access the route to this 'admin' template and if not boot them back to their account page. I use the 'profilej' variable for this.
I then get the Profile that needs to be updated and I use the 'profileq' variable for this.
When I print these out they contain the appropriate values. 'profilej' contains the superuser and 'profileq' contains the Profile to be edited.
#login_required(login_url='login')
def editAccountBoss(request, pk):
profilej = request.user.profile
print(profilej)
if profilej.bossnum != 9999:
return redirect('account')
profileq = Profile.objects.get(id=pk)
print(profileq)
form = ProfileFormBoss(instance=profileq)
In the last line of the above code, I am passing the 'profileq' to the form variable.
It is then this same 'form' that I am validating and saving in the code below.
The Problem I keep getting is that the 'superuser' (who is the sessionid authenticated user) is getting updated instead of the Profile that opens up and is edited.
This 'edit profile' code works fine when any sessionid authenticated profile/user is updating their ownn profile so i dont think its trying to create a duplicate and yet I am getting the following error and behind the scenes before the error, the fields of 'profileq' are being updated to the superuser 'profilej'.
here is the rest of the code:
if request.method == 'POST':
form = ProfileFormBoss(request.POST, request.FILES, instance=profileq)
websocial = request.POST['social_website']
#print(websocial[0:8])
if websocial[0:8] != "https://" and websocial != "":
messages.error(request, 'Use full web address format:
https://www.example.com')
elif form.is_valid():
profileq = form.save(commit=False)
profileq.is_accepted = True
form.save()
return redirect('acc-boss-dash-profiles')
context = {'form': form}
return render(request, 'users/profile_formboss.html', context)
This is the error I keep getting. I have messed around with a few different alternatives (i.e. profileq.save() etc.) but keep getting same error:
IntegrityError at /profiles/edit-account/
UNIQUE constraint failed: auth_user.username
And here is the underlying Model Form:
class ProfileFormBoss(ModelForm):
class Meta:
model = Profile
fields = ['name', 'email', 'username',
'location', 'bio', 'short_intro', 'profile_image', 'is_accepted', 'social_github', 'social_linkedin', 'social_twitter', 'social_youtube', 'social_website']
exclude = ['social_github', 'social_linkedin', 'social_twitter',
'social_youtube']
help_texts = {
'social_website': 'https://www.example.com',
}
widgets = {
'isaccepted': forms.CheckboxSelectMultiple(),
}
def __init__(self, *args, **kwargs):
super(ProfileFormBoss, self).__init__(*args, **kwargs)
for name, field in self.fields.items():
field.widget.attrs.update({'class': 'input'})
ADDED below is the Profile model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
bossnum = models.PositiveSmallIntegerField(default=0)
is_accepted = models.BooleanField(default=False)
name = models.CharField(max_length=200, blank=True, null=True)
email = models.EmailField(max_length=500, blank=True, null=True)
username = models.CharField(max_length=200, blank=True, null=True)
location = models.CharField(max_length=200, blank=True, null=True)
short_intro = models.CharField(max_length=200, blank=True, null=True)
bio = models.TextField(blank=True, null=True)
profile_image = models.ImageField(null=True, blank=True, upload_to='profiles/', default="profiles/user-default.png")
social_github = models.CharField(max_length=200, blank=True, null=True)
social_twitter = models.CharField(max_length=200, blank=True, null=True)
social_linkedin = models.CharField(max_length=200, blank=True, null=True)
social_youtube = models.CharField(max_length=200, blank=True, null=True)
social_website = models.CharField(max_length=200, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
def __str__(self):
return str(self.username)
class Meta:
ordering = ['created']
#property
def imageURL(self):
try:
url = self.profile_image.url
except:
url = ''
return url
ADDED below is code from a Dashboard template that displays a list of Profiles (that need approval by the admin Profile).
The following 'a' tag is displayed with each Profile in the list of Profile cards.
The 'a' tag successfully opens the url 'edit-account-boss' with the correct Profile displayed (via the 'profilex.id' being passed to 'pk' in the 'editAccountBoss' view)
<a class="tag tag--pill tag--main settings__btn" href="{% url 'edit-account-boss' profilex.id %}"><i class="im im-edit"></i> Edit</a>
ADDED below is the form from the template that opens when an above 'a' tag is clicked (....this is the form with/causing the original error and problem of updating the admin Profile instead of the user Profile intended to be updated):
NOTE: following comment by #Chris , i noticed that the action attribute should perhaps be 'edit-account-boss' in lieu of 'edit-account'. This may be the solution but at present causes another error. If the following action attribute is updated to 'edit-account-boss', then clicking an above 'a' tag, gives this error immediately:
NoReverseMatch at /profiles/edit-accountboss/5195f3ed-4d83-40c6-a3d7-d31da603c47c/
Reverse for 'edit-account-boss' with no arguments not found. 1 pattern(s) tried: ['profiles/edit\-accountboss/(?P[^/]+)/\Z']
<form class="form" action="{% url 'edit-account' %}" method="POST" enctype='multipart/form-data'>
{% csrf_token %}
{% for field in form %}
<div class="form__field">
<label for="formInput#text">{{field.label}} </label>
{{field}}
</div>
{% endfor %}
<input class="btn btn--sub btn--lg my-md" type="submit" value="Submit" />
</form>
ADDED below are the urls (including: 'edit-account' and 'edit-account-boss'):
urlpatterns = [
path('login/', views.loginUser, name="login"),
path('logout/', views.logoutUser, name="logout"),
path('register/', views.registerUser, name="register"),
path('', views.profiles, name="profiles"),
path('profile/<str:pk>/', views.userProfile, name="user-profile"),
path('account/', views.userAccount, name="account"),
path('account-bossdash/', views.accountBossDash, name="acc-boss-dash"),
path('account-bossdashprofiles/', views.accountBossDashProfiles, name="acc-boss-dash-profiles"),
path('edit-account/', views.editAccount, name="edit-account"),
path('edit-accountboss/<str:pk>/', views.editAccountBoss, name="edit-account-boss"),
path('create-skill/', views.createSkill, name="create-skill"),
path('update-skill/<str:pk>/', views.updateSkill, name="update-skill"),
path('delete-skill/<str:pk>/', views.deleteSkill, name="delete-skill"),
path('inbox/', views.inbox, name="inbox"),
path('message/<str:pk>/', views.viewMessage, name="message"),
path('create-message/<str:pk>/', views.createMessage, name="create-message"),
]
#Chris - you solved this - thankyou!! (i.e. action="{% url 'edit-account-boss' profileq.id %}" ) - and - 'profileq.save()' - will you post the answer so I can give it the green tick. Thankyou!!!
<form class="form" action="{% url 'edit-account-boss' profileq.id %}" method="POST" enctype='multipart/form-data'>
{% csrf_token %}
{% for field in form %}
<div class="form__field">
<label for="formInput#text">{{field.label}} </label>
{{field}}
</div>
{% endfor %}
<input class="btn btn--sub btn--lg my-md" type="submit" value="Submit" />
</form>
Then also in the view, instead of form.save() it needed profileq.save()
elif form.is_valid():
profileq = form.save(commit=False)
profileq.save()
I'm a little new to Django, so my question may be basic, but bare with me. Let's say I'm sharing my posts with some of my friends (who are manytomany relation with the post), but also giving an extra condition for whether they can comment on it, or not. This condition, together with the name of the user to be shared with are submitted through a forum. Now my problem is that I don't know how/where to save this condition. I will show you some of my code.
Models.py
class Task(models.Model):
name = models.CharField(max_length=50)
text = models.TextField()
deadline = models.DateTimeField()
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='taskset')
shared = models.ManyToManyField(User, blank=True, related_name = 'shared_list')
def __str__(self):
return f"{self.name}-{self.user.username}"
class Comment(models.Model):
text = models.TextField()
task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comments')
created = models.DateField(auto_now_add=True)
def __str__(self):
return f"{self.user.username}-{self.task}"
Share form html
{% extends 'base.html'%}
{% load crispy_forms_tags %}
{% block content %}
<h3>Enter the Username of the Friend you want to share with</h3>
<form method="POST">
{% csrf_token %}
{{form|crispy}}
<input type="submit", value="Share">
</form>
{% endblock content %}
And the view processing it
def share(request, id):
task = Task.objects.get(id = id)
if request.method == 'POST':
share_with = User.objects.get(username = request.POST['username'])
if share_with is not None:
task.shared.add(share_with)
task.save()
return redirect('index')
else:
form = ShareForm()
return render(request, 'share.html', {'form':form})
Thanks a lot, I've been stuck for two hours, PLEASE HELP!
I have a model called 'Competicion', with some objects and another model called 'Participante'. This second model has two fields: a foreignkey with the user and another foreingkey to 'Competicion'.
In the view, I've made queryset from 'Competicion' and with a for loop in the template I've given each object the button of the form.
With storing the user of the current session I have no problem but I want the form to know which object of the queryset it is to grab its id. #I have no problem with choices I just don't include them to save space
Models.py
class Competicion(models.Model):
ciudad = models.CharField(max_length=50, null=True, choices=Ciudades_a_elegir, default="Ninguna")
nombre = models.CharField(max_length=20, null=True, choices=Competiciones_a_elegir, default="Ninguna")
titulo = models.CharField(max_length=40, null=True)
fecha = models.DateField(null=True)
participantes = models.IntegerField(null=True)
flyer = models.ImageField(null=True, upload_to='imagenes', default='Ninguna')
def __str__(self):
return self.nombre + " " + self.titulo
class Participante(models.Model):
competicion = models.ForeignKey(Competicion, on_delete=models.CASCADE, null=True, blank=True)
participantes = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.competicion.nombre + " " + self.competicion.titulo
forms.py
class ParticipanteForm(ModelForm):
class Meta:
model = Participante
exclude = ['competicion', 'participantes']
views.py
def competiciones(request):
qs = Competicion.objects.all()
form = ParticipanteForm(request.POST or None)
if form.is_valid():
nombres = form.save()
nombres.competicion = ???#Help me
nombres.participantes = request.user
nombres.save()
return redirect('home')
context = {
'object_list':qs,
'form':form,
}
return render(request, "competiciones.html", context)
template
{% for i in object_list %}
<ul>
<li>{{i.nombre}}</li>
<li>{{i.ciudad}}</li>
<li>{{i.titulo}}</li>
<li>{{i.fecha}}</li>
<li>{{i.participantes}}</li>
{% if i.flyer %}
<img src="{{i.flyer.url}}" width="100px" >
{% endif %}
<li><form action="" method="POST">
{% csrf_token %}
<p> {{ form.competicion}} </p>
<p>{{form.participantes}}</p>
<input type="submit" name="Inscribirse" value="Inscribirse">
</form> </li>
</ul>
{% endfor %}
This is only like 1 course. In number one there is the different fields of the Competicion Model. And number two is the button of the form to the Participante Model, which fields are hidden and take the id of the course and the user. So I have a lot of these both courses displayed in the web. The function of the Particpante Model is to store the people who register in the course and the course itself.
def competiciones(request):
qs = Competicion.objects.all()
form = ParticipanteForm(request.POST or None)
if form.is_valid():
data = form.save()
data.participantes_id = request.user
for i in qs:
data.competicion_id = i.id
data.save()
return redirect('home')
I need to add data to the database For that, I'm trying some code. But I can't add foreignkey values. The code throws exceptions. Here are my views.py, models.py code and exceptions.
first try:
views.py
def notification(request):
user = request.user
if request.method == 'POST':
property_id = request.POST['property_id']
owner = request.POST['owner_id']
property_object = Property.objects.get(id=property_id)
owner_object = Property.objects.get(owner=owner)
notification = user, "have intrested in your property"
property_object.notify.add(user)
notifications = Notifications.objects.create(notification=notification, owner=owner_object
,property=property_object)
notifications.save()
it throws exception
ValueError: Field 'id' expected a number but got 'hafis'.
second try
views.py
def notification(request):
user = request.user
if request.method == 'POST':
property_id = request.POST['property_id']
owner = request.POST['owner_id']
property_object = Property.objects.get(id=property_id)
notification = user, "have intrested in your property"
property_object.notify.add(user)
notifications = Notifications.objects.create(notification=notification, owner=owner,
property=property_id)
notifications.save()
it throws exception
ValueError: Cannot assign "'hafis'": "Notifications.owner" must be a "User" instance.
models.py
class Property(models.Model):
owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
headline = models.CharField(max_length=255)
city = models.CharField(max_length=100)
location = models.CharField(max_length=100)
facilites = models.TextField()
rent = models.CharField(max_length=200)
images = models.FileField(upload_to='media/uploads',null=True)
email = models.EmailField()
mobile = models.IntegerField(null=True)
date = models.DateTimeField(auto_now_add=True)
notify = models.ManyToManyField(User, default=None, blank=True, related_name='Liked')
def __str__(self):
return self.headline
class Notifications(models.Model):
owner = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE)
property = models.ForeignKey(Property, null=True, blank=True,on_delete=models.CASCADE)
notification = models.TextField()
date = models.DateTimeField(auto_now_add=True)
def __str_(self):
return self.notification
HTML code
{% if accomodations %}
{% for accomodation in accomodations %}
<form action="{% url 'notification' %}" method="post">
{% csrf_token %}
<input type="hidden" name="owner_id" value="{{ accomodation.owner }}">
<button id="request-btn" name="property_id" value="{{ accomodation.id }}" class="btn btn-primary">Contact info:</button>
</form>
You need to pass the users instance. You can get user by this line of code
owner = User.objects.all().get(username=owner)
Note:
Every user should have differnt usernames
ValueError: Cannot assign "'hafis'": "Notifications.owner" must be a "User" instance. you're just passing in the ID but it requires it to be a User object.
def notification(request):
user = request.user
if request.method == 'POST':
property_id = request.POST['property_id']
owner = User.objects.get(id=request.POST['owner_id'])
property_object = Property.objects.get(id=property_id)
notification = user, "have intrested in your property"
property_object.notify.add(user)
notifications = Notifications.objects.create(notification=notification, owner=owner,
property=property_id)
notifications.save()
I've already read many other threads complaining about this error message but I still can't figure this out. I try removing the fields that give the error, and the error message just moves to another field the next time I try to submit. They are CharField, Foreign Key, and other types.
forms.py
class TemporaryresponseForm(forms.ModelForm):
gender_custom = forms.CharField(
required=False,
label="",
)
ethnicity = forms.ModelChoiceField(
queryset=Ethnicity.objects.all(),
widget=forms.RadioSelect(),
empty_label=None,
required=True,
label="Which of the following best describes your ethnicity?"
)
...
class Meta:
model = Temporaryresponse
fields = [...'gender_custom', 'ethnicity',...]
views.py
def tr(request):
if request.method == "POST":
form = TemporaryresponseForm(request.POST)
if form.is_valid():
tempresponse = form.save(commit=False)
tempresponse.ip = "123456"
tempresponse.save()
return redirect('politicalpollingapp/index.html')
else:
form = TemporaryresponseForm()
return render(request, 'politicalexperimentpollapp/tr.html', {'form': form})
def nr(request, pk):
return render(request, 'politicalexperimentpollapp/nr.html', {'tempresponse': tempresponse})
tr.html template
{% extends 'politicalexperimentpollapp/base.html' %}
{% block extrahead %}
{% load crispy_forms_tags %}
{{ form.media }}
{% endblock extrahead%}
...
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<div><br></div>
<div class="text-center"><button type="submit" class="save btn btn-primary">CONTINUE</button></div>
</form>
..
models.py
class Ethnicity(models.Model):
ethnicity = models.CharField(max_length=200)
def __str__(self):
return '%s' % (self.ethnicity)
...
class Temporaryresponse(models.Model):
birth_year = models.PositiveIntegerField()
voting_registration = models.ForeignKey(Voting_registration, models.SET_NULL, null=True)
party_identification = models.ForeignKey(Party_identification, models.SET_NULL, null=True)
gender = models.ForeignKey(Gender, models.SET_NULL, null=True)
gender_custom = models.CharField(max_length=200, blank=True)
ethnicity = models.ForeignKey(Ethnicity, models.SET_NULL, null=True)
race = models.ManyToManyField(Race)
zip_code = models.IntegerField()
ip = models.CharField(max_length=200, blank=True)
policy_areas_of_importance = models.ManyToManyField(Policy_category, blank=True)
likelihood_of_voting = models.PositiveIntegerField(models.SET_NULL, null=True, blank=True)
Oddly no error shows up in my Chrome console - it's only because I am showing errors on the actual page. I'm not sure if that's normal. Thanks in advance for any help, I'm ripping my hair out at this point.
I discovered that I was using the wrong language for the "race" form field. I had used ModelChoiceField but it should be ModelMultipleChoiceField as follows:
race = forms.ModelMultipleChoiceField(queryset=Race.objects.all(), widget=forms.CheckboxSelectMultiple, label="5. Which of the following best describes your race? Please select all that apply.")