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
silly question about sessions

I'm using sessions in a current django project and recently got a 'Object of type 'date' is not JSON serializable' error.

Reading through the django docs on sessions I'm not clear why sessions use a serializer?

(I'm using the db to store the session)
From my understanding isn't just the session ID going to the browser in a cookie and the session data is stored in the db? The data in the session can be accessed via the ORM like a normal model. However when I look at the database table there is just one field, session_data (apart from expiry date and session id). Looking at the docs seems to be signed (but not encrypted? - not sure of the difference). So I'm not clear where the serializer is being used?

Additionally I was using django wizard before which stores intermediate data (such as the field causing the date issue above) in the session. I've now switched that part of the app to use seperate views for flexibility (signup wasn't just a linear path), but now have the 'not JSON serializable' error, how does django wizard get round this?

/r/djangolearning
https://redd.it/6x70xe
Search form with ManyToManyFields

I am creating a basic CRUD application using Django for the first time. I would love to have some guidance and direction regarding how to add a search form.

I need to create a search form for a `ServiceProvider` model. The search form has three text boxes to search for service providers by any or all of `location`, `memberships` and `expertise`. The latter two search fields are of `ManyToManyField` type.

These are the model definitions I have:

class ServiceProvider(models.Model):
provider_name = models.CharField(max_length=200)
created_date = models.DateTimeField(default=timezone.now)

# SEARCH FIELDS ->
location = models.CharField(max_length=100)
memberships = models.ManyToManyField(Memberships)
expertise = models.ManyToManyField(SkillAreas)


class Memberships(models.Model):
membership_name = models.CharField(max_length=200)
membership_description = models.TextField(blank=True, null=True)


class SkillAreas(models.Model):
skill_name = models.CharField(max_length=200)
skill_description = models.TextField(blank=True, null=True)



Though `memberships` is a multi select field in the backend, in the search field, user can only type one skill at the moment. Same goes for `expertise` field. How should I go about adding a search form for this `ServiceProvider`?

I found a solution for search for multiple fields in a stackoverflow [answer](https://stackoverflow.com/a/26640293): [Adding search to a Django site in a snap](https://www.julienphalip.com/blog/adding-search-to-a-django-site-in-a-snap/). In this solution, I didn't quite understand what Q is. Does this solution fit my need at all?

I also learned that there is something called `django-haystack`, but it feels a bit complex than the above solution and also I am not sure if it fits my need. Thanks in advance.


/r/djangolearning
https://redd.it/6wae1j
Conda can only be run in super

Hey everybody, just started to pick up Python and Django, and the class I'm following tells me to use conda to set up my venv. After a while of not getting anaconda to run, I fixed my .bash_profile so it can run, but when I run ``` $ conda``` it gives me the following error:

PermissionError: [Errno 13] Permission denied: '/Users/teddykoomen/anaconda3/lib/python3.6/site-packages/numba-0.33.0-py3.6-macosx-10.7-x86_64.egg-info/PKG-INFO'

When I run conda using ```$ sudo conda``` it does not give me the same error. I don't want to have to run conda as super every time. Any ideas? I've looked over stackoverflow and the anaconda docs and I couldn't find anything helpful. Thank you!

/r/Python
https://redd.it/6xci8y
Pycharm and Flask

Hi everyone! I wanted to learn flask for quite a time and decide now to dive into it. I’ve written some simple and small apps with VS Code and the terminal. Now I want to grew for the next step: Databases

I find it quite hard to get a grasp on it(where exactly to start, what do I have write for code, how do I set up a database etc.) but I noticed pyCharm has a flask and database integration.

So I opend my flask project in pyCharm and try run it (which of course not work because of the flask run command (I guess)).

Is there a way to get the project work with flask without setting a complete new flask project?

I use a virtualenv with flask and all modules installed. Also tried to google for an solution but everything is just about setting up a new project. ;(

Thanks for your help!


Edit: Thank you for the answers! The main error was a missing:


if __name__ == '__main__':
app.run()

/r/flask
https://redd.it/6x6vdy
What data type is returned from request.form.get when applied to a html "date" input ?



/r/flask
https://redd.it/6x6bqx
new to class based views and have a question

I have a working app and am playing around with class based views and I want to add a check on agency_count in AgencyFullView. Here's my working views without the check:

class AgencyFullMixin(ContextMixin):

def get_context_data(self, pk, **kwargs):
context_data = super(AgencyFullMixin, self).get_context_data(**kwargs)
agency = Agencies.objects.filter(pk=pk)
context_data["agency"] = agency
agency_count = agency.count()
context_data["agency_count"] = agency_count

return context_data

class AgencyFullView(TemplateView, AgencyFullMixin):

template_name = 'myapp/agency_full.html'

def get_context_data(self, **kwargs):
context_data = super(AgencyFullView, self).get_context_data(**kwargs)
return context_data

What I want to do is add a check on the agency_count and if it's 0 direct to one template, if not, to another template. Basic stuff but I'm not quite sure how to do it here. I tried the following but I get "name 'agency_count' is not defined" error :

class AgencyFullView(TemplateView, AgencyFullMixin):

if agency_count == 0:
template_name = 'myapp/not_valid.html'
else:
template_name = 'myapp/agency_full.html'

def get_context_data(self, **kwargs):
context_data = super(AgencyFullView, self).get_context_data(**kwargs)
return context_data





/r/django
https://redd.it/6xht2k
How to Show Calculated Field in Django Admin

Let's say I have a model Invoice.

class Invoice(models.Model):
labor_hour_quantity = models.DecimalField(max_digits=9, decimal_places=1)
labor_descrip = models.TextField(blank=True)
labor_hour_cost = models.DecimalField(max_digits=9, decimal_places=2)

Within the model I have a property defined as such.

@property
def labor_total(self):
return self.labor_hour_quantity * self.labor_hour_cost

How do I get this displayed on Django Admin when I look at an invoice I create? Currently it doesn't even display a DecimalField or value of the return after saving the invoice.

/r/django
https://redd.it/6xigr3
Flask-mail adding data to email

Hello again guys i have a invoice that im generating but in that invoice i want to add data from database how can i add that data to a template to be send as a email, i am using render_template() but how to make does values persist when that email is sent.

render_template('template.html', var='var')

In the template I'm calling

{{var}} # but it doesn't display the value of var in gmail

Thanks in advance.

/r/flask
https://redd.it/6xfvn7