hi am working on a project where am using multiple user data a user did a post onto the site and when driver see that post he adds their offer to that post but when driver submit the post ...at the admin level the particular is selected automatically but the post is not selected on which he adds price this is my post model.py
class Loader_post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE ,related_name="Loader")
pick_up_station = models.CharField(max_length=150)
destination_station = models.CharField(max_length=150)
sender_name = models.CharField(max_length=150)
phone_number = PhoneNumberField(null=False, blank=False, unique=True)
receiver_name = models.CharField(max_length=150)
this is my second model of adding price to a particular post
class price(models.Model):
my_post = models.ForeignKey(Loader_post, related_name='prices')
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, default='')
driver_price = models.CharField(max_length=150, null=True)
driver_name = models.CharField(max_length=150, null=True)
status = models.BooleanField(default=False)
this is my adding price to the post views.py
#login_required
def add_price_to_post(request, pk):
post = get_object_or_404(Loader_post, pk=pk)
user = request.user
if request.method == "POST":
form = price_form(request.POST)
if form.is_valid():
ps = form.save(commit=False)
ps.user = request.user
ps.status = True
ps.post = post
ps.save()
return redirect('Driver:Driverview')
else:
form = price_form()
return render(request, 'price_form.html', {'form': form})
this is my html add post button
{% for loader in Loader %}
this is loop and this is button
add price
you can see my_post is select automatically
In your model the field name is my_post while in the add_price_to_post you are adding ps.post. change that to ps.my_post.
Related
I am trying to convert my current project(It is developed in Django) to DRF. So, I set up DRF in my project then I wrote an endpoint for after user login on the session I need to get the response when I test the my-reviews API.
models.py
class customer(models.Model):
cust_id = models.IntegerField(null="true")
email = models.CharField(max_length=100)
# reemail = models.CharField(max_length=100, null='true')
password = models.CharField(max_length=500)
repassword = models.CharField(max_length=500, null='true')
firstname = models.CharField(max_length=225)
lastname = models.CharField(max_length=225, null=True)
state = models.CharField(max_length=64, null=True)
city = models.CharField(max_length=64, null=True)
location = models.CharField(max_length=225, null=True)
Zip = models.CharField(max_length=64)
mailing = models.CharField(max_length=1000)
added_date = models.DateTimeField(editable=False)
modified_date = models.DateTimeField(null=True, blank=True)
last_loggedin = models.DateField()
views.py
#api_view(['GET'])
def myservicereviewAPI(request):
# If a user session is logged out it will redirect to the home page.
if ((request.session.get('email') is None) or (request.session.get('email') == "")):
# redirecting the user after logging out to the home page.
return HttpResponseRedirect("/home")
if request.method == 'GET':
students = services_review.objects.all().order_by('-added_date')
serializer = ServicesReviewSerializer(students, many=True)
return Response(serializer.data)
urls.py
path('myservicereviewAPI', views.myservicereviewAPI, name='myservicereviewAPI'),
Results of Postman when I run 'myservicereviewAPI'
After login Browser results of 'myservicereviewAPI'
Please Help me to achieve this.
When I clicked the publish button. I get this error (image field = `This field is required), and it's not submitting the post.
models.py:
class Blog(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='post_author')
blog_title = models.CharField(max_length=300, verbose_name='Put a Title')
slug = models.SlugField(max_length=264, unique=True, null=True)
blog_content = models.TextField(verbose_name='What is on your mind')
blog_image = models.ImageField(upload_to='blog_images', verbose_name='Image', null=True)
publish_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.blog_title
views.py:
#login_required
def createblog(request):
form = CreateBlogPost()
if request.method == 'POST':
form = CreateBlogPost(request.POST, request.FILES)
if form.is_valid():
blog_obj = form.save(commit=False)
blog_obj.author = request.user
title = blog_obj.blog_title
print(title)
blog_obj.slug = title.replace(" ", "-")+"-"+str(uuid.uuid4())
print(blog_obj.slug)
blog_obj.save()
return HttpResponseRedirect(reverse('index'))
return render(request, 'App_Blog/create_blog.html', {'form': form})
This field is required this is a Validation error from Django form and it depends on your models, if you are using model form you have to set blank=True like this
blog_image = models.ImageField(upload_to='blog_images', verbose_name='Image', null=True,blank=True)
blank=True means field can be empty (it is not required field) and null=True means your database table column can accept null values.
I created models called Interview, Users, Interview_interviewer like wise...
Interview_interviewer table has foreign keys from other models.
I just want to save data from both 2 tables to Interview_interviewer(Without django forms) table which is many to many table. So I just created the views and template for it. When button clicks it save the Interviewers to table along side with the interview. But when do it, It gave me and error called "User matching query does not exist".
/home/govinda/DMG/test3/myapp/views.py in hod_inter_interviewer_2
usr = User.objects.get(id=pid)
What should I do?
class Interview(models.Model):
Time = models.TimeField()
Date = models.DateField()
Venue = models.ForeignKey('Venue')
HOD = models.ForeignKey(User)
Vacancy = models.ForeignKey('Vacancy', on_delete=models.CASCADE)
Department = models.ForeignKey(Department, on_delete=models.CASCADE)
InterviewType = models.ForeignKey(InterviewType, on_delete=models.CASCADE)
Interviewer_Review = models.TextField(blank=True, null=True)
HOD_Review = models.TextField(blank=True, null=True)
HR_Review = models.TextField(blank=True, null=True)
NoOfPasses = models.PositiveIntegerField(blank=True, null=True)
NoOfFails = models.PositiveIntegerField(blank=True, null=True)
NoOfOnHolds = models.PositiveIntegerField(blank=True, null=True)
InterviewNo = models.IntegerField(blank=True, null=True)
Post = models.ForeignKey(Post, on_delete=models.CASCADE)
and
class Users(models.Model):
User = models.OneToOneField(User)
FullName = models.CharField(max_length=100)
Post = models.ForeignKey(Post)
UPhoto = models.FileField(upload_to=User_directory_path,null = True,blank=True)
Department = models.ForeignKey(Department)
UserRole = models.ForeignKey(UserRole)
def __str__(self):
return u'{}'.format(self.User)
and
class Interview_Interviewer(models.Model):
Interview = models.ForeignKey(Interview)
Interviewer = models.ForeignKey(User)
def __str__(self):
return u'{}'.format(self.Interviewer)
views are...
def hod_pre_interviwer_list(request, iid):
inter = Interview.objects.get(id=iid)
a = UserRole.objects.get(Role="Interviewer")
viewer = Users.objects.filter(UserRole=a.id)
return render(request, 'hod_inter_create_2.html', {'viewer': viewer, 'inter': inter, 'a':a})
def hod_inter_interviewer_2(request, iid, pid):
inter = Interview.objects.get(id=iid)
usr = User.objects.get(id=pid)
a = UserRole.objects.get(Role="Interviewer")
viewer = Users.objects.filter(UserRole=a.id)
usr_id = Users.objects.get(User=a.id)
inter_id = inter
person_id = usr_id
form = Interview_Interviewer(Interview=inter_id, Interviewer=person_id)
form.save()
return render(request, 'hod_inter_create_2.html', {'viewer': viewer, 'inter': inter})
urls are...
urlpatterns = [
url(r'^hod/hod_vacancy/test/part2/inter_list/(\d+)/$', hod_pre_interviwer_list, name="inter1"),
url(r'^hod/hod_vacancy/test/part2/inter_list/(\d+)/(\d+)/$', hod_inter_interviewer_2, name="inter2"),
]
template is...
<a type="submit" class="btn btn-primary" href="/hod/hod_vacancy/test/part2/inter_list/{{ inter.id }}/{{ viewer.id }}">Add</a>
Try using named groups in your url patterns
urlurl(r'^hod/hod_vacancy/test/part2/inter_list/?P<iid>[0-9]+)/?P<pid>[0-9]+/$', hod_inter_interviewer_2, name="inter2"),
If that doesn't work then i suggest trying User.object.get(pk=pid) as in most doc examples.
And make sure that there is a user with that id (iid) in the url.
You should also use get_object_or_404 for getting any single object from a model in the view as it gives a more user friendly and appropriate error.
I would like the slug to be generated automatically from the title the user imports, or have random integers generated if the title is blank. The way I currently have it, the slug is supposed to be populated from the form title, but I get an error saying the form doesn't have a title field.
So, I need to:
change the slug in the views.py to be populated from the inputed title
create a random integer generator if the title is blank (I'm assuming in models.py and views.py?)
Could someone please help me with this?
Thank you in advance! I appreciate any and all help!
forms.py:
class PhotoUploadForm(forms.ModelForm):
title = forms.CharField(
widget=forms.TextInput(attrs={"placeholder": "Title of photo", "size": "30"})
)
description = forms.CharField(
widget=forms.Textarea(attrs={"placeholder": "Description of photo"})
)
class Meta:
model = Photo
fields = ('category', 'title', 'description', 'image')
models.py:
class Photo(models.Model):
creator = models.ForeignKey(MyUser, null=False, blank=False)
title = models.CharField(max_length=30, null=True, blank=True)
description = models.TextField(max_length=120, null=True, blank=True)
image = models.ImageField(upload_to='user/photos/', null=True, blank=True)
slug = models.SlugField(null=False, blank=False)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
class Meta:
ordering = ['-timestamp']
def __unicode__(self):
return "%s" %(self.creator)
views.py:
def photo_upload_view(request, username):
u = MyUser.objects.get(username=username)
form = PhotoUploadForm()
if request.method == 'POST':
form = PhotoUploadForm(request.POST or None, request.FILES)
new_slug = Photo.objects.get(slug=form.title)
if form.is_valid():
obj = form.save(commit=False)
obj.creator = request.user
obj.slug = slugify(new_slug)
obj.save()
messages.success(request, "Thank you! You have successfully posted your picture!")
return HttpResponseRedirect('/')
else:
form = PhotoUploadForm()
submit_btn = "Upload Post"
context = {
"submit_btn": submit_btn,
"form": form
}
return render(request, "photos/photo_upload.html", context)
This line is your problem:
new_slug = Photo.objects.get(slug=form.title)
You're setting the value of new_slug to a retrieved object, not a string. That isn't going to slugify. You probably just want this:
new_slug = form.title
But if you actually want to retrieve a value from a Photo, reference that attribute at the end, so:
new_slug = Photo.objects.get(slug=form.title).slug
As for generating a random integer:
from random import randint
randint(0,100)
Will return a random integer between 0 and 100 (inclusive).
I have a view which validates data from a form which just has some basic information about an item. I'm confused with how the is_valid method works here even after reading
this . If the user doesn't input some of the required fields like name or image 1, I want them to see the error on the page "this field is required" or something of that nature. I thought if the form.is_valid returned False, these messages would automatically be raised on the page for the user. Or do I need to specify what error message for each field somewhere that I would want the user see?
#view
def sell(request):
if request.method == "POST":
form = AddItem(request.POST, request.FILES)
if form.is_valid():
item = form.save(commit=False)
item.user = request.user
item.is_active = True
item.slug = slugify(item.name)
item.save()
return HttpResponseRedirect('thanks.html')
else:
form = AddItem()
return render_to_response('forsale.html', locals(), context_instance=RequestContext(request))
#form
class AddItem(forms.ModelForm):
name = forms.CharField(label="Title")
class Meta:
model = Item
exclude = ('user','slug','is_active',)
#model
class Item(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=75)
slug = models.SlugField(max_length=50, unique=True)
is_active = models.BooleanField(default=True, blank=True)
image1 = models.ImageField(upload_to='img')
image2 = models.ImageField(upload_to='img', blank=True)
image3 = models.ImageField(upload_to='img', blank=True)
image_caption1 = models.CharField(max_length=200, blank=True)
image_caption2 = models.CharField(max_length=200, blank=True)
image_caption3 = models.CharField(max_length=200, blank=True)
price = models.DecimalField(max_digits=8, decimal_places=2)
quantity = models.IntegerField(default=1)
description = models.TextField()
created = models.DateTimeField(auto_now_add=True)
shipping_price = models.DecimalField(decimal_places=2, max_digits=6)
categories = models.ManyToManyField(Category)
You need to extract the errors from the form object using form.errors then deal with the dict however you want. If you're using ajax, simply send the dict as json back over and use javascript to handle it. If it was a direct html form submit, then you need to render and respond with a page with the errors in the passed dictionary and deal with the passed error in the template (usually with an {% if errors %} tag