Redirect page to dynamic URL Django - python

I'm a beginner on using django platform to create a mini project website and use a function based view. One of the feature in my project is a profile page that allowing user to edit the personal information. Here is my user profile models.py:
class UserProfile(models.Model):
user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE, blank=True)
company_name = models.CharField(max_length=50, blank=True)
company_url = models.URLField(max_length=50, blank=True)
country = CountryField(null=True)
AFF_CHOICES = (('Academic','Academic'),
('Business','Business'),
('Non-profit','Non-profit'),
('Industry/Government','Industry/Government'))
affiliation = models.CharField(max_length=50, choices=AFF_CHOICES, null=True, blank=True)
profile_picture = models.ImageField(upload_to='profile_picture', null=True, default='profile_picture/defaultphoto.png')
def __str__(self):
return str(self.user)
Here is my urls.py
app_name = 'dashboard'
urlpatterns = [
path('', dashboard, name='dashboard'),
path('<int:pk>/profile/',profile, name='profile'),
path('<int:pk>/edit_profile/',edit_profile, name='edit_profile'),
as you can see it will pass user PK so it will look like this (example on profile page) http://127.0.0.1:8000/dashboard/42/profile/ and it's already works. Here is my profile and edit_profile views.py:
def profile(request, pk):
return render(request, 'dashboard/profile.html')
def edit_profile(request, pk):
user = request.user
form = UserProfileForm(instance=user)
if request.method == 'POST':
form = UserProfileForm(request.POST, request.FILES, instance=user,)
if form.is_valid():
form.save()
messages.success(request, 'Your profile has been updated.')
return redirect('/profile/dashboard/') #The problem is here #Still static
else :
form = UserProfileForm()
context = {'editform':form}
return render(request, 'dashboard/editprofile.html', context)
i've tried this case with static url (not passing the user PK on url) and it is already works. How do i pass the user PK correctly after user edited their profile. (I want to redirect them to /dashboard/user:pk/profile). Thanks

try this:
return redirect('dashboard:profile', pk=pk)

Try this:
from django.urls import reverse
# In your view
return redirect(reverse('dashboard:profile', kwargs={'id': pk}))

Related

How to automatically get user in django admin through form

I have a form in my django website where the user requests coins and the information is sent to the admin for me to process. I want to automatically get the user who filled the form without them doing it themselves.
Here's the model.py file:
class Requestpayment (models.Model):
username= models.ForeignKey(User, on_delete= models.CASCADE, null=True)
useremail= models.CharField(max_length=100)
accountmail= models.CharField(max_length=100)
accountphonenumber=models.CharField(max_length=15)
coinsrequested=models.ForeignKey(Requestamount, on_delete= models.SET_NULL, null=True)
created= models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.accountmail
the forms.py:
class Requestpaymentform (ModelForm):
class Meta:
model = Requestpayment
fields = '__all__'
and the views.py:
#login_required(login_url='login')
def redeemcoins (request):
form = Requestpaymentform
if request.method =='POST':
form = Requestpaymentform(request.POST)
if form.is_valid():
form = form.save(commit=False)
username = request.user
form.save()
return redirect ('home')
I am pretty sure something is wrong but i don't know what it is (I'm very new at django) anyway the form always shows all the users in the website for the current user to pick who they are.
redeem coins page
I also tried excluding that part of the form but it didn't work it just shows up empty in the admin.
thank you.
You need to assign it to the instance wrapped in the form, so:
#login_required(login_url='login')
def redeemcoins(request):
form = Requestpaymentform()
if request.method == 'POST':
form = Requestpaymentform(request.POST)
if form.is_valid():
form.instance.username = request.user
form.save()
return redirect('home')
# …
It makes more sense however to name this field user than username. In the model you can also make the username field non-editable, such that it does not appear in the form:
from django.conf import settings
class Requestpayment(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, editable=False, on_delete=models.CASCADE
)
# …
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
When you use username= models.ForeignKey(User, on_delete= models.CASCADE, null=True), Django add a field named user_id in your database which allow django to find User object for Requestpayment.
You can use user_id field to add a User object in Requestpayment.
You don't need to pass username field in your fields list if you want to get user in view.
class Requestpaymentform (ModelForm):
class Meta:
model = Requestpayment
#fields = '__all__'
fields = ['useremail',
'accountmail',
'accountphonenumber',
'coinsrequested',
'created']
Now do this to get user in your view.
#login_required(login_url='login')
def redeemcoins(request):
form = Requestpaymentform()
if request.method == 'POST':
form = Requestpaymentform(request.POST)
if form.is_valid():
requestpayment = form.save(commit=False)
requestpayment.user_id = request.user.id
requestpayment.save()
return redirect('home')
And it's great to use user instead username because it's a User object and not a simple field.
Please for my English !!!

How can I save a django Model Form in views for the logged in user

I am working on a project in Django where I have an Education(models.Model) with a OneToOneField to User Model as shown below:
class Education(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
qualification = models.CharField(max_length=60, choices=INSTITUTE, blank=True, null=True)
institution = models.CharField(max_length=40, null=True)
reasons = models.CharField(max_length=100, null=True)
matnumber = models.CharField(max_length=255, null=True)
And a forms.ModelForm as shown below:
class AddEducationForm(forms.ModelForm):
class Meta:
model = Education
fields = ['qualification','instition', 'matnumber', 'reasons','refphone']
In my views.py file I want to save this AddEducationForm for the logged in user. Below is what I have tried but it is not saving but showing success message of save.
def AddEducation(request):
if request.method == 'POST':
form = AddEducationForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
messages.success(request, 'Education Added Successfully')
return redirect('user-bank')
else:
form = AddEducationForm()
context = {
'form':form,
}
return render(request, 'user/add_education.html', context)
The system is displaying the success message that the form has been saved for the logged in user but in reality it is not (when checked using Django Admin Login).
Someone should kindly help out with the solution to this problem. Remember each logged in user would save only one Education Form record. Thank in anticipation.
You add it to the instance of the form, so:
from django.contrib.auth.decorators import login_required
#login_required
def add_education(request):
if request.method == 'POST':
form = AddEducationForm(request.POST, request.FILES)
if form.is_valid():
form.instance.applicant = request.user
form.save()
messages.success(request, 'Education Added Successfully')
return redirect('user-bank')
else:
form = AddEducationForm()
context = {
'form':form,
}
return render(request, 'user/add_education.html', context)
Note: You can limit views to a view to authenticated users with the
#login_required decorator [Django-doc].
Note: Functions are normally written in snake_case, not PascalCase, therefore it is
advisable to rename your function to add_education, not addEducation.
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

Django - how to make a CloudinaryField not mandatory and how to allow user to delete their uploaded image?

Hej, I am just beginning to learn Django and I want my users to be able to upload a profile picture, so I extended the User model with a Profile model, however, so far I have not figured out how to make the image upload non-mandatory. And when the user has added a picture, I would like them to be able to remove it as well.
Screenshot - Not able to Submit without adding file
Screenshot - Not able to remove the added file
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField()
profile_image = CloudinaryField('profile_image')
def __str__(self):
return self.user.username
forms.py
class ProfileForm(ModelForm):
class Meta:
model = Profile
fields = ('bio', 'profile_image',)
views.py
#login_required(login_url='login')
def createPost(request):
form = PostForm()
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
# request.FILES necessary so that file is submitted
# also required to add enctype to the form
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('home')
context = {'form': form}
return render(request, 'my8gag/post_form.html', context)
Couldn't find anything about this in the documentation or here on SO, so would appreciate any kind of help/hints! Thanks in advance.
Allow the field to be null
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField()
profile_image = CloudinaryField('profile_image', null=True, default=None, blank=True)
def __str__(self):
return self.user.username

Django ModelForm does not save in database

I am trying to make a simple to-do list in Django that each user could have their own task list so when they logged in they add a task and its save for themselves and the list only display their own tasks, but when I try to add a task from the template's form it won't save but when I add task manually from admin panel it work.
my models.py
from django.db import models
from django.contrib.auth.models import User
class Tasks(models.Model):
user = models.ForeignKey(User, null=True,on_delete=models.CASCADE)
title = models.CharField(max_length=200)
check = models.BooleanField(default = False)
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
forms.py
from django import forms
from django.forms import ModelForm
from .models import *
class TaskForm(forms.ModelForm):
class Meta:
model = Tasks
fields = '__all__'
views.py:
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .forms import *
from .models import Tasks
#login_required(login_url = 'login')
def tasks(request):
tasks = Tasks.objects.filter(user = request.user)
context = { 'tasks': tasks }
return render(request,'ToDo/list.html',context)
#login_required(login_url = 'login')
def add_task(request):
form = TaskForm()
if request.method == 'POST':
form = TaskForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.user = request.user
form.save()
return redirect('/')
context = {'form' : form}
return render(request,'ToDo/add.html',context)
where is the problem?
You assign the user to the .user attribute of the form, not of the .instance wrapped in the form. You thus should alter the instance with:
#login_required(login_url = 'login')
def add_task(request):
if request.method == 'POST':
form = TaskForm(request.POST, request.FILES)
if form.is_valid():
form.instance.user = request.user
form.save()
return redirect('/')
else:
form = TaskForm()
return render(request, 'ToDo/add.html', {'form' : form})
You should furthermore only redirect in case of a successful POST request: in case the POST request is not successful, the form can render the error messages, and thus will inform the user what the problem is.
Furthermore you make the user field non-editable:
from django.conf import settings
class Tasks(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
editable=False,
on_delete=models.CASCADE
)
title = models.CharField(max_length=200)
check = models.BooleanField(default = False)
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

Django trouble using image form

Ive been trying to create a profile page for a project, where the user can upload a profile pic and some basic personal information using Django as framework.
The text based information I pass in my code seem to work, but I cant get the user to upload a profile pic from the profile page. But if I go to django-admin page, I can upload and display the profile pic from there. So to models.py file seem to work, and the issue seem to be in forms.py file... Ive search the net for some time and various turtorials, but nothing seem to work. Please help :)
models.py file
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
email_confirmed = models.BooleanField(default=False)
image= models.ImageField(upload_to='profile_image', blank=True)
def __str__(self):
return self.user.username
forms.py file
class ProfileUpdateForm(forms.ModelForm):
YEARS= [x for x in range(1900,2021)]
birth_date = forms.DateField( initial="21-06-1995", widget=forms.SelectDateWidget(years=YEARS))
image = models.ImageField(upload_to='profile_image',blank=True)
class Meta:
model = Profile
fields = ('bio','birth_date','location','image')
views.py file
#login_required
def profile_edit(request):
form = ProfileUpdateForm(request.POST)
if request.method == 'POST':
if form.is_valid():
user = request.user
user.profile.bio = form.cleaned_data.get("bio")
user.profile.birth_date = form.cleaned_data.get("birth_date")
user.profile.location = form.cleaned_data.get("location")
user.save()
return redirect('profile')
else:
form = ProfileUpdateForm()
context = {
'form' : form
}
return render(request, 'webside/profile_edit.html', context)
Static root/url and Media root/url has been implemented in settings and Ive added the static url to the urls.py file. Any help would be greatly appreciated :)
You're only passing the data to the form, not the files:
form = ProfileUpdateForm(request.POST)
Change to:
form = ProfileUpdateForm(data=request.POST, files=request.FILES)
And save your form instead of assigning the data to a user object. You're not saving the user.profile when you do user.save():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
You did not save your image in view after posting from your form object. You save it like this if form is valid
if 'image' in request.FILES:
user.profile.image = request.FILES['image']

Categories