My Django model for video upload saying “ValueError” - python-3.x

I am trying to write a small blog where I can be uploading my videos for public download but am getting a server error message when I try click on any video for details. Below is the error that I'm getting when Debug is set to True
ValueError at /video/lagos-anthem/
Sample larger than population or is negative
Request Method: GET
Request URL: https://www.majestylink.com/video/lagos-anthem/
Django Version: 3.1.2
Exception Type: ValueError
Exception Value:
Sample larger than population or is negative
Exception Location: /home/majestyempire/.virtualenvs/majestyenv/lib/python3.7/random.py, line 321, in sample
Python Executable: /usr/local/bin/uwsgi
Python Version: 3.7.5
Python Path:
['/var/www',
'.',
'',
'/var/www',
'/home/majestyempire/.virtualenvs/majestyenv/lib/python37.zip',
'/home/majestyempire/.virtualenvs/majestyenv/lib/python3.7',
'/home/majestyempire/.virtualenvs/majestyenv/lib/python3.7/lib-dynload',
'/usr/lib/python3.7',
'/home/majestyempire/.virtualenvs/majestyenv/lib/python3.7/site-packages',
'/home/majestyempire/musicblog/myblog']
Server time: Sat, 28 Nov 2020 13:49:35 +0100
Below is my models.py
class Video(models.Model):
CATEGORY_CHOICES = (
('Music', 'Music'),
('Movies', 'Movies'),
)
artist = models.CharField(max_length=200, unique=True)
category = models.CharField(max_length=30, choices=CATEGORY_CHOICES, default='Music')
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(default='', blank=True, unique=True)
thumbnail = models.ImageField(blank=False)
video_file = models.FileField(default='')
uploaded_date = models.DateTimeField(default=timezone.now)
objects = PostManager()
class Meta:
ordering = ['-uploaded_date']
def save(self):
self.uploaded_date = timezone.now()
self.slug = slugify(self.title)
super(Video, self).save()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('video:detail', kwargs={'slug': self.slug})
This is the post_detail view
def post_detail(request, slug):
random_posts = random.sample(list(Video.objects.all()), 2)
vid = get_object_or_404(Video, slug=slug)
comments = Comment.objects.filter(post=vid)
is_liked = False
context = {
'Video': Video,
'vid': vid,
'random_posts': random_posts,
}
return render(request, 'video/video_detail.html', context)

def post_detail(request, slug):
a = Video.objects.all().count()
if a > 2:
random_posts = random.sample(list(Video.objects.all()), 2)
else:
random_posts = random.choices(list(Video.objects.all()))
vid = get_object_or_404(Video, slug=slug)
comments = Comment.objects.filter(post=vid)
is_liked = False
context = {
'Video': Video,
'vid': vid,
'random_posts': random_posts,
}
return render(request, 'video/video_detail.html', context)

Related

EcomUser() got an unexpected keyword argument 'mobile_number'

I'm trying to create a user and link it with the user profile. But here is the problem.
This is my profile model
class EcomUser(BaseModelMixin):
profile = models.OneToOneField(
settings.AUTH_USER_MODEL, unique=True, blank=True, null=True,
on_delete=models.CASCADE)
username = models.CharField(max_length=100, blank=True)
email = models.CharField(max_length=100, blank=True, null=True)
name = models.CharField(max_length=100, blank=True)
referral_code = models.CharField(max_length=10, blank=True)
signinId = models.CharField(max_length=20, blank=True)
GOOGLE = 'GOOGLE'
APPLE = 'APPLE'
MOBILE_NUMBER = 'mobile_number'
LOGIN_TYPES = (
(GOOGLE, 'GOOGLE'),
(APPLE, 'APPLE'),
(MOBILE_NUMBER, 'mobile_number')
)
loginType = models.CharField(max_length=15, choices=LOGIN_TYPES, default=MOBILE_NUMBER)
class Meta:
db_table = "ecom_user"
My user model:
class User(AbstractBaseUser, BaseModelMixin):
email = models.EmailField(
verbose_name='email_address',
max_length=255,
blank=True,
null=True
)
username = models.CharField(max_length=128, null=True,unique=True)
fullName = models.CharField(max_length=256, blank=True)
mobile_number = models.CharField(max_length=15, blank=True,unique=True)
is_superuser = models.BooleanField(default=False)
class Meta:
db_table = "users"
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = [] # Email & Password are required by default.
def get_full_name(self):
# The user is identified by their email address
return self.fullName
def __str__(self): # __unicode__ on Python 2
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
objects = UserManager()
and serializer.py
class UserRegisterSerializer(serializers.Serializer):
"""Normal serializer which accepts mobile number"""
mobile_number = serializers.CharField(max_length=10, min_length=10, required=True)
referral_code = serializers.CharField(max_length=10, min_length=10, required=True)
email = serializers.EmailField(max_length=255)
def validate_referral_code(self, referral_code):
if not re.match(r"^[\w]{10}$", referral_code):
raise serializers.ValidationError("Wrong Referral Code")
return referral_code
def validate_mobile_number(self, mobile_number):
if not re.match(r"^[\d]{10}$", mobile_number):
raise serializers.ValidationError("Please pass proper mobile number")
return mobile_number
class EcomUserSerializer(serializers.ModelSerializer):
profile_id = serializers.IntegerField(required=True)
mobile_number = serializers.CharField(max_length=10, min_length=10, required=True)
referral_code = serializers.CharField(max_length=10, min_length=10, required=True)
class Meta:
model = EcomUser
fields= ('profile_id','referral_code', 'mobile_number', 'loginType', 'signinId')
def create(self, data):
instance = EcomUser.objects.create(**data)
return instance
views.py
#api_view(['POST'])
def registration(request):
try:
local_reponse = {
"status": False,
"message": "",
"code": status.HTTP_400_BAD_REQUEST,
"data": []
}
data = request.data
print('1')
mobile_number = data.get('mobile_number', None)
referralCode = data.get('referralCode', '')
LoginType = data.get('LoginType')
signinId = data.get('signinId', '')
email = data.get('email', '')
print(f'{LoginType} --- {EcomUser.MOBILE_NUMBER}')
if LoginType == "mobile_number":
print('2')
exist_user = User.objects.filter(mobile_number=mobile_number).count()
if exist_user:
print('3')
local_reponse["message"] = msg.ALREADY_EXIST
return Response(local_reponse)
try:
print('not found')
registerd_user = User.objects.get(mobile_number=mobile_number)
print(f'{registerd_user} -- check')
if registerd_user:
print('register')
ecom = EcomUser.objects.get(profile_id=registerd_user.id)
if ecom:
local_reponse["message"] = msg.ALREADY_EXIST
except User.DoesNotExist:
print('4')
user = User.objects.create_ecom_user(mobile_number=mobile_number)
user.save()
print('is user create?')
if user:
print('yes')
info = {}
info['profile_id'] = user.id
info['referral_code'] = referralCode
info['mobile_number'] = mobile_number
info['LoginType'] = LoginType
info['signinId'] = signinId
ecom_serializer = EcomUserSerializer(data=info)
if not ecom_serializer.is_valid():
print('5')
local_reponse["message"] = ecom_serializer.error_messages
local_reponse["status"] = False,
return Response(local_reponse)
print(f'linked profile 1 ----' )
ecomuser_data = ecom_serializer.save()
print(f'linked profile 2 ---- ' )
ecomuser_id = ecomuser_data.id
print('linked profile 3' )
ecomuser = EcomUser.objects.get(id=ecomuser_id)
print('linked profile 4' )
ecom_serializer = EcomUserSerializer(ecomuser)
print('linked profile 5' )
context = ecom_serializer.data
print('linked profile 6' )
local_reponse = {
"status": True,
"message": msg.CREATED,
"code": status.HTTP_201_CREATED,
"data": context
}
print('linked profile 7' )
return Response(local_reponse)
except Exception as e:
print('7')
local_reponse["message"] = str(e)
return Response(local_reponse)
When I try to run API on postman. It showed "EcomUser() got an unexpected keyword argument 'mobile_number'" and the output in the terminal show below:
1
mobile_number --- mobile_number
2
not found
4
is user create?
yes
linked profile 1 ----
7
The user is created but can not link to the profile, any one can help?
There's no mobile_number field in your EcomUser model. Thus when you call
ecomuser_data = ecom_serializer.save()
after adding
info['mobile_number'] = mobile_number
if fails, leaving you with the user and no profile. I guess is_valid() is only checking the fields it wants, not detecting the surplus, but the extra field causes the save to fail with the error shown. You've already added the field when creating the user, so you don't need it in the profile, so removing that second line should make things work.

extract signature from SignatureField in Django

here i am using python3 and Django 3.0
Here am i saving the signature into my database and now i need to display this signature in my pdf file
But i am not able to display it in the pdf file
here is my views.py
def jobspecific_view_1(request, pk):
form = CustomerSignatureForm(request.POST or None)
if form.is_valid():
customer_signature_1 =
form.cleaned_data.get('customer_signature_1')
if customer_signature_1!=None:
job = AddJob.objects.get(id=pk)
job.customer_signature_1 = customer_signature_1
job.save()
......
......
here is my views.py for generating the pdf
def render_to_pdf(template_src, context_dict, pdf_title):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result,
encoding='UTF-8')
if not pdf.err:
response = HttpResponse(result.getvalue(), content_type='application/pdf')
response['Content-Disposition'] = "attachment; filename={0}".format(
unidecode(
pdf_title.replace(
',', '').replace(
';', '').replace(' ', '_')))
logger.debug('Content-Disposition: {0}'.format(response['Content-Disposition']))
return response
logger.error(pdf.err)
return HttpResponse('We had some errors<pre>%s</pre>' % cgi.escape(html))
def generate_pdf_view(request, pk):
client = request.user.client
job_id = AddJob.objects.filter(id=pk)
viewed_job = get_object_or_404(AddJob, id=pk, created_by__client=client)
job_data={}
for val in job_id:
job_data['job_id'] = pk
job_data['title'] = val.title
job_data['job_number'] = val.job_number
job_data['customer_signature_1'] = val.customer_signature_1
......
......
pdf_title = u'{0}_{1}_{2}.pdf'.format(
job_data['title'], job_date.strftime('%d_%m_%Y'),
job_data['job_type'])
return render_to_pdf('jobs/jobpdf.html',
{
'pagesize':'A4', 'job_data': job_data,
'viewed_job': viewed_job,
'request': request,
}, pdf_title)
Here is my forms.py
class CustomerSignForm(forms.ModelForm):
customer_signature_1 = SignatureField(required=False,label="Customer Signature")
class Meta:
model = AddJob
fields = ['customer_signature_1']
Here is my jobspdf.html
<img src="data:image/png;base64,{{job_data.custmer_signature_1}}"/>
Please help me so that i can display the signature in my pdf view at present it not displaying anything in my pdf

Django Query data optimization

Recently, I watched Django and discovered the teaching videos of select_related and prefetch_related.
So I installed debug_toolbar and took a look at my website
I searched the database too many times on one page.
I must convert it to json and send it back to the front end
Can I still optimize?
Is this the only way to go?
Below is my model .
models.py
def get_upload_path(instance, filename):
return f'register/{instance.owner.job_number}/{filename}'
def image_upload_path(instance, filename):
date = datetime.datetime.strptime(instance.date, '%Y-%m-%d %H:%M:%S')
return f'image/{date.year}/{date.month}/{date.day}/{filename}'
class UserProfile(models.Model):
name = models.CharField(max_length=64)
department = models.CharField(max_length=32)
job_number = models.CharField(max_length=32, unique=True)
card_number = models.CharField(max_length=32, unique=True)
location = models.CharField(max_length=32)
date = models.DateTimeField()
class UserRegister(models.Model):
owner = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
date = models.DateTimeField()
feature = ArrayField(models.FloatField(null=True, blank=True), null=True, blank=True, size=512)
image = models.ImageField(upload_to=get_upload_path)
class Meta:
db_table = 'UserRegister'
class UserImage(models.Model):
owner = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
heat = models.CharField(max_length=8, blank=True, null=True)
date = models.DateTimeField()
image = models.ImageField(upload_to=image_upload_path)
punch_in = models.CharField(max_length=8, blank=True, null=True)
class Meta:
db_table = 'UserImage'
views.py
def get_db_data(db, page, limit, model):
data = []
paginator = Paginator(db, limit)
try:
page_object = paginator.page(page)
all_page = paginator.num_pages
for db_data in page_object:
images = []
for image in db_data.userregister_set.all():
try:
url = f'/static/uploads{image.image.url}'
except ValueError:
url = '/static/imgs/assets/none.jpg'
images.append({'id': image.id, 'url': url})
_ = {
'id': db_data.id,
'name': db_data.name,
'department': db_data.department,
'job_number': db_data.job_number,
'card_number': db_data.card_number,
'location': db_data.location,
'date': db_data.date.strftime('%Y-%m-%d %H:%M:%S'),
'images': images,
}
data.append(_)
result = {
'data': data,
'all_page': all_page,
'page': int(page)
}
return result
except EmptyPage as error:
# 該頁數沒資料。
return {'msg': 'Error'}
class Test(Views):
def get(self, request):
name = request.GET.get('name')
department = request.GET.get('department')
job_number = request.GET.get('job_number')
card_number = request.GET.get('card_number')
location = request.GET.get('location')
start_date = request.GET.get('start_date')
end_date = request.GET.get('end_date')
order = request.GET.get('order', default='DESC')
page = request.GET.get('page', default=1)
limit = request.GET.get('limit', default=25)
user_profile = UserProfile.objects.all().select_related()
if name:
user_profile = user_profile.filter(name=name)
if department:
user_profile = user_profile.filter(department=department_table.department)
if job_number:
user_profile = user_profile.filter(job_number=job_number)
if card_number:
user_profile = user_profile.filter(card_number=card_number)
if location:
user_profile = user_profile.filter(location=locations_table.location)
if start_date and end_date:
user_profile = user_profile.filter(date__range=[start_date, end_date])
if order == 'DESC':
data = get_db_data(db=user_profile.order_by('-date'), page=page, limit=limit, model='api')
else:
data = get_db_data(db=user_profile.order_by('date'), page=page, limit=limit, model='api')
response = JsonResponse(data)
# return response # This is the response I want to return, but now I need to use `debug_toolbar`, so I return an empty page to facilitate my reading `debug_toolbar`.
return render(request, 'User/test1.html')
return JsonResponse(data) is what I want to return, but now I need to use debug_toolbar, so I return to an empty page to facilitate reading debug_toolbar.
Use select_related() or prefetch_related().
Put the associated parameters into.
can speed up the ORM.

IntegrityError at /images/create/ NOT NULL constraint failed: images_image.users_like_id

I'm working on a project from the book 'Django3 by example'. I want to add an image from the URL which the user provided. But when I try to save the page it gives me this error:
IntegrityError at /images/create/
NOT NULL constraint failed: images_image.users_like_id.
I have tried the solutions other posted but it didn't help.
Here is my code:
models.py
from django.db import models
from django.conf import settings
from django.utils.text import slugify
class Image(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
related_name='images_created')
users_like = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
related_name='images_liked')
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=200, blank=True)
url = models.URLField()
image = models.ImageField(upload_to='images/%Y/%m/%d/')
description = models.TextField()
created = models.DateField(auto_now_add=True, db_index=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super().save(*args, **kwargs)
views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import ImageCreateForm
#login_required
def image_create(request):
if request.method == "POST":
form = ImageCreateForm(data=request.POST)
if form.is_valid():
cd = form.cleaned_data
new_item = form.save(commit=False)
new_item.user = request.user
new_item.save()
messages.success(request, 'Image added successfully')
return redirect(new_item.get_absolute_url())
else:
form = ImageCreateForm(data=request.GET)
context = {'section':'images', 'form':form}
return render(request, 'images/image/create.html', context)
forms.py
from django import forms
from urllib import request
from django.core.files.base import ContentFile
from django.utils.text import slugify
from .models import Image
class ImageCreateForm(forms.ModelForm):
class Meta:
model = Image
fields = ('title', 'url', 'description')
widgets = {
'url': forms.HiddenInput,
}
def clean_url(self):
url = self.cleaned_data['url']
valid_extension = ['jpg', 'jpeg']
extension = url.rsplit('.', 1)[1].lower()
if extension not in valid_extension:
raise forms.ValidationError('The given URL does not match valid image extension')
return url
def save(self, force_insert=False, force_update=False, commit=True):
image = super().save(commit=False)
image_url = self.cleaned_data['url']
name = slugify(image.title)
extension = image_url.rsplit('.', 1)[1].lower()
image_name = f'{image}.{extension}'
response = request.urlopen(image_url)
image.image.save(image_name, ContentFile(response.read()), save=False)
if commit:
image.save()
return image
I'm using a ForeignKey on users_like instead of ManyToManyField as a user might
like multiple images and each image can be liked by multiple users.

AssertionError in unit testing

while I was writing tests I got that error
assert response.status_code == status.HTTP_201_CREATED
E AssertionError: assert 415 == 201
E -415
E +201
models.py
class Product(TimeStamp):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
color = models.ManyToManyField(ColorOfProduct)
available = models.BooleanField(default=True)
image = models.ImageField(upload_to='products/%Y/%m/%d', null=True, blank=True)
slug = models.SlugField(max_length=100, unique=True, blank=True)
def __str__(self):
return f'{self.category} - {self.name}'
tests.py looks like this
class ProductTest(APITestCase):
def post_product(self, category, name, brand_name, color, available):
url = reverse(ProductView.name)
print(url)
data = {
'category': category,
'name': name,
'brand_name': brand_name,
'color': color,
'available': available,
}
response = self.client.post(url, data, format='json')
return response
def test_post_and_get_product(self):
new_category_name = 'Hewlet Packard'
new_product_name = 'HP Zenbook'
new_brand_name = 'HP'
new_color = 'black'
new_available = True
response = self.post_product(
new_category_name,
new_product_name,
new_brand_name,
new_color,
new_available,
)
assert response.status_code == status.HTTP_201_CREATED
assert Product.objects.count() == 1
assert Product.objects.get().name == new_product_name
I am indicating media type but when I check it throws error "Unsupported media type"
how can I solve this issue?
The reason might be django's default test format is application/octet-stream. You need to use json.dumps() before calling your test method. In your post_product method, just change:
response = self.client.post(url, data, format='json')
to
import json
response = self.client.post(url, json.dumps(data), content_type='application/json')

Resources