Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Does anyone know how I can fix this? I haven't used docker in the last few months (3-4) and yesterday I updated my docker and now this is happening. Now I don't know if is this because of an update or something else.

https://redd.it/zzx5dr
@pythondaily
I want to know how I can create a invoice of data obtained from database (admin panel) and print it using custom django functions



/r/django
https://redd.it/zzx9xl
Django redirect doesn't work when trailing slash is there.

So I have a url pattern which looks like this

urlpatterns = [
path('register-user/', views.registerUser, name='registerUser'),
path('register-vendor/', views.registerVendor, name='registerVendor'),
path('login/', views.login, name='login'),
path('logout/', views.logout, name='logout'),
path('dashboard/', views.dashboard, name='dashboard'),
path('vendor-dashboard/', views.vendorDashboard, name='vendorDashboard'),
path('my-account/', views.myAccount, name='myAccount'),
]

and views

def myAccount(request):
user = request.user
redirectUrl = detectUser(user)
return redirect(redirectUrl)

def dashboard(request):
return render(request, 'accounts/dashboard.html')

def vendorDashboard(request):
return render(request, 'accounts/vendor-dashboard.html')

the detect user function detects and user type and returns respective URL

def detectUser(user):
if user.role == 1:
redirectUrl = 'vendor-dashboard/'

elif user.role == 2: redirectUrl = 'dashboard/'

elif user.role == None and user.is_superadmin:
redirectUrl = 'admin'

return redirectUrl

but when I see the website the URL is

/r/django
https://redd.it/zzspi2
Help with identifying Users uniquely without Log in

Hello I have built a simple quote website for fun using Django Rest Framework and React js and I want to add a LIKE function to it but I want it to be without Logging in.
How can i achieve this with Django? How can i uniquely identify the Anonymous User without asking for Login In.

/r/django
https://redd.it/zzt3gu
export to excel(.xls)

i am using python xlwt module to convert my database query into an excel sheet.everything works fine,but now i want to adjust my date coloumn as below

def tonerdetails_export_toexcel(request,id):
toner_model = find_toner_model(id)
fmt = '%Y-%m-%d %H:%M:%S'
response =HttpResponse(content_type='application/ms-excel')
response['Content-Disposition']='attachment; filename=TonerReport ' + str(toner_model) + '.xls'
wb=xlwt.Workbook(encoding='utf=8')
ws=wb.add_sheet('Toner Report')
row_num=0
font_style=xlwt.XFStyle()
font_style.font.bold=True
date_format = xlwt.XFStyle()
date_format.num_format_str = 'yyyy/mm/dd'
columns=['Toner Model','Issued To','Employee Name','Employee Designation','Date Dispatched','Status']
for col_num in range (len(columns)):
ws.write(row_num,col_num,columns[col_num],font_style)
font_style = xlwt.XFStyle()
rows=TonerDetails.objects.filter(toner_model_id=id).values_list('toner_model_id__toner_model','issued_to_id__name','employee_name',


/r/django
https://redd.it/zzp8f4
Flask run fail after Python update

I updated my Python from version 3.9.2 to 3.11.1 on a Windows 10 machine. I reinstalled Flask and other modules I needed for the new Python version through pip and it seems that my apps work as normal when I use python -m flask run. However just flask run still looks for the old (and now uninstalled) Python 3.9 and of course fails as the system can't find the files. How can I fix this? I tried uninstalling it and reinstalling it with no success.

One think I have noticed, but I have no idea if this relevant or not, is that the old Python was installed under C:\\Program Files while the new one is only installed in AppData/Local even though I installed it for all Users. Plus I can't change the installation path in the setup program, even when running it as Administrator.

/r/flask
https://redd.it/1002wun
Operational error after changing the operating system

Dear Flask developers,

\-I built my flask app on WSL(Windows Subsystem for Linux) without a problem and it is working on WSL.

\-In order to test my app on a server environment I copied my app to an OpenBSD virtual machine on Hyper-V.

\-Ran my app on the local network.

\-I tried to connect from a different machine to the server(OpenBSD VM). Connected without a problem.

\-There is a login screen in my app and when I try to login as a user I got an operational error:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: Users
SQL: SELECT "Users".id AS "Users_id", "Users".email AS "Users_email", "Users".password AS "Users_password", "Users".option_1 AS "Users_option_1", "Users".option_2 AS "Users_option_2", "Users".option_3 AS "Users_option_3"
FROM "Users"
WHERE "Users".email = ?
LIMIT ? OFFSET ?
parameters: ('test_1@test.com', 1, 0)
(Background on this error at: https://sqlalche.me/e/14/e3q8)

​

Traceback (most recent call last)

File "/home/user/.local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1900, in executecontext
self.dialect.doexecute(
File "/home/user/.local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 736, in do
execute
cursor.execute(statement, parameters)

Could

/r/flask
https://redd.it/zzuv8x
Deploying an application to “production” in AWA

I have a Django app. I totally messed up it’s configuration and I can’t get it running. Honestly, I have no idea how I got in running in the first place a year ago. I’m not comfortable at all with Apache etc.

I’d like to reset it up from scratch. Are there any great, detailed tutorials. I need to use my custom domain and SSL as well.

Please help :)

/r/django
https://redd.it/1006r4u
Sunday Daily Thread: What's everyone working on this week?

Tell /r/python what you're working on this week! You can be bragging, grousing, sharing your passion, or explaining your pain. Talk about your current project or your pet project; whatever you want to share.

/r/Python
https://redd.it/10073zi
Is it possible to set the USERNAME_FIELD to email without having to build a custom manager and forms from scratch?

Im trying to setup a user model with an added birthday field and remove the username field. Can I do this using the usercreation forms and built in manager? Or would I still have to create my own from scratch

/r/djangolearning
https://redd.it/100ai5k
Creating a webp image on post_save

Hi everyone,

I've figured out how to create a webp image in my /media/ folder within the correct directory. Now, I'm needing to save this image to a field that i've crated on my model.

def create_webp_image(sender, instance, *args, **kwargs):

image_url = instance.image.thumbnail['1920x1080'].url

path = "http://localhost:8000" + image_url
response = requests.get(path, stream=True)

img = Image.open(BytesIO(response.content))

#build filepath
position = path.rfind("/") + 1
newpath2 = path[0:position].split('/media/')[1]

#get name of file (eg. picture.webp)
name_of_file = path[position:].split('.')[0] + ".webp"

#get directory
image_dir = os.path.join(settings.MEDIA_ROOT, '')


/r/django
https://redd.it/1005ij1
Workaround ALLOWED_HOSTS for a specific route for startup probe?

I am deploying a Django app to a kubernetes cluster. I want it to have a startup probe, which will basically just be a view that checks the database connection and returns a 200 response.

I'm having trouble because kubernetes will make a request directly to the pod IP with the pods hostname. Additionally, the kubernetes config is kind of out of my hands. I'd rather solve the problem by having a looser Django view than monkeying with the infrastructure code.

Is there a way for a single route / view to ignore the hosts whitelist?

/r/django
https://redd.it/1001y06
connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "rootuser" does not exist



\`I am trying to connect deploy a django backend with postgres db on digitalocean droplet. but it's giving me this error after after gunicorn and nginx setup:

`connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "rootuser" does not exist`

rootuser is my root username not the db username, my db username is dbadmin

i tried to create db user with name serveroot, it worked but started throwing other errors

`\`relation "django_site" does not exist LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...`

/r/django
https://redd.it/zyze4t
Count the Comment ID over again when created in different Post

Hello!
I would like to know if it possible and if it is, how make the ID of the Comment to start to count over again when created in a different Post.
Thank you!


Models:
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)

def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk })
class Comment(models.Model):
post = models.ForeignKey(Post,related_name='comments', on_delete=models.CASCADE)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)

/r/djangolearning
https://redd.it/100irg7
Monday Daily Thread: Project ideas!

Comment any project ideas beginner or advanced in this thread for others to give a try! If you complete one make sure to reply to the comment with how you found it and attach some source code! If you're looking for project ideas, you might be interested in checking out Al Sweigart's, "The Big Book of Small Python Projects" which provides a list of projects and the code to make them work.

/r/Python
https://redd.it/100xk64