Missing rows in Pandas
Hi all,
I used Pandas to create data frames to split a dataset into various age ranges, the age range is 0 - 95 in total.
I removed any rows which were over the age of 95 which gave a new total of 110,456 using df.loc, the total number of rows only comes to 106,917 meaning some have been uncounted:
zeroTo14 = hosp_df.loc[(hosp_df['Age'] > 0) & (hosp_df['Age'] <= 14)]
fifteenTo29 = hosp_df.loc[(hosp_df['Age'] >= 15) & (hosp_df['Age'] <= 29)]
thirtyTo44 = hosp_df.loc[(hosp_df['Age'] >= 30) & (hosp_df['Age'] <= 44)]
fortyfiveTo59 = hosp_df.loc[(hosp_df['Age'] >= 45) & (hosp_df['Age'] <= 59)]
sixtyTo64 = hosp_df.loc[(hosp_df['Age'] >= 60) & (hosp_df['Age'] <= 64)]
sixtyfiveTo74 = hosp_df.loc[(hosp_df['Age'] >= 65) & (hosp_df['Age'] <= 74)]
seventyfiveTo89 = hosp_df.loc[(hosp_df['Age'] >= 75) & (hosp_df['Age'] <= 89)]
nintetyTo89 = hosp_df.loc[(hosp_df['Age'] >= 90)]
I think I may have screwed up the greater than and less than symbols as I need to count every single age in between 0 and 95.
I am very grateful for any help here please, more eyes the better. Thanks
/r/pystats
https://redd.it/8pxqat
Hi all,
I used Pandas to create data frames to split a dataset into various age ranges, the age range is 0 - 95 in total.
I removed any rows which were over the age of 95 which gave a new total of 110,456 using df.loc, the total number of rows only comes to 106,917 meaning some have been uncounted:
zeroTo14 = hosp_df.loc[(hosp_df['Age'] > 0) & (hosp_df['Age'] <= 14)]
fifteenTo29 = hosp_df.loc[(hosp_df['Age'] >= 15) & (hosp_df['Age'] <= 29)]
thirtyTo44 = hosp_df.loc[(hosp_df['Age'] >= 30) & (hosp_df['Age'] <= 44)]
fortyfiveTo59 = hosp_df.loc[(hosp_df['Age'] >= 45) & (hosp_df['Age'] <= 59)]
sixtyTo64 = hosp_df.loc[(hosp_df['Age'] >= 60) & (hosp_df['Age'] <= 64)]
sixtyfiveTo74 = hosp_df.loc[(hosp_df['Age'] >= 65) & (hosp_df['Age'] <= 74)]
seventyfiveTo89 = hosp_df.loc[(hosp_df['Age'] >= 75) & (hosp_df['Age'] <= 89)]
nintetyTo89 = hosp_df.loc[(hosp_df['Age'] >= 90)]
I think I may have screwed up the greater than and less than symbols as I need to count every single age in between 0 and 95.
I am very grateful for any help here please, more eyes the better. Thanks
/r/pystats
https://redd.it/8pxqat
reddit
r/pystats - Missing rows in Pandas
2 votes and 3 so far on reddit
Class based view: Best generic view
Hi guys, I'm new to the class based view concept. I'm looking for the best approach to accomplish the following task:
A page containing a form and two inline forms.
The form should contain the first item from a query.
There should be three buttons for:
Delete the current item.
Update the current item.
Autofill values and update the current object.
After all options it should load a page with the next item in the query.
I already have a function view for this, so I know already how I accomplish most of this task.
What I know need to know is if I can use generic views (I would need multiple, am I correct?) or would it be the best to use a Formview, overwrite the post function and put all three button logics into it? The last one would be quite identical to my current function view. I'm just no fan of putting everything in a huge function.
/r/djangolearning
https://redd.it/8ps1dh
Hi guys, I'm new to the class based view concept. I'm looking for the best approach to accomplish the following task:
A page containing a form and two inline forms.
The form should contain the first item from a query.
There should be three buttons for:
Delete the current item.
Update the current item.
Autofill values and update the current object.
After all options it should load a page with the next item in the query.
I already have a function view for this, so I know already how I accomplish most of this task.
What I know need to know is if I can use generic views (I would need multiple, am I correct?) or would it be the best to use a Formview, overwrite the post function and put all three button logics into it? The last one would be quite identical to my current function view. I'm just no fan of putting everything in a huge function.
/r/djangolearning
https://redd.it/8ps1dh
reddit
r/djangolearning - Class based view: Best generic view
6 votes and 1 so far on reddit
[R] A simple example for data augmentation of time-series data
https://github.com/terryum/Data-Augmentation-For-Wearable-Sensor-Data
/r/MachineLearning
https://redd.it/8px7r2
https://github.com/terryum/Data-Augmentation-For-Wearable-Sensor-Data
/r/MachineLearning
https://redd.it/8px7r2
GitHub
GitHub - terryum/Data-Augmentation-For-Wearable-Sensor-Data: A sample code of data augmentation methods for wearable sensor data…
A sample code of data augmentation methods for wearable sensor data (time-series data) - terryum/Data-Augmentation-For-Wearable-Sensor-Data
What to test in Django model? (apart from custom/overridden attributes)
What do you usually test or recommend to test regarding Django models? **This question relates specifically to testing anything on top of your custom and overridden attributes of the model.**
For instance, there is a general advice:
> If the code in question is built into Django, don't test it.
However, I think it is reasonable to write a test for each model that:
1. Creates an instance of a model, including saving it in test database.
2. Checking if count of objects in test database increased by 1.
This approach helps you ensure that your models are successfully validated and are, well... *creatable*!
/r/django
https://redd.it/8pzo41
What do you usually test or recommend to test regarding Django models? **This question relates specifically to testing anything on top of your custom and overridden attributes of the model.**
For instance, there is a general advice:
> If the code in question is built into Django, don't test it.
However, I think it is reasonable to write a test for each model that:
1. Creates an instance of a model, including saving it in test database.
2. Checking if count of objects in test database increased by 1.
This approach helps you ensure that your models are successfully validated and are, well... *creatable*!
/r/django
https://redd.it/8pzo41
reddit
r/django - What to test in Django model? (apart from custom/overridden attributes)
2 votes and 1 so far on reddit
Any good tutorial on how to deploy a local Django app to heroku?Following documentation keeps throwing error
I tried following the documentation but when I do it with my app I get errors on
git push heroku master
I am not too experienced with github either.Any good start to finish guides for beginners?
/r/django
https://redd.it/8py2cr
I tried following the documentation but when I do it with my app I get errors on
git push heroku master
I am not too experienced with github either.Any good start to finish guides for beginners?
/r/django
https://redd.it/8py2cr
reddit
Any good tutorial on how to deploy a local Django app... • r/django
I tried following the documentation but when I do it with my app I get errors on git push heroku master I am not too experienced with github...
Confused on forms (ModelForms) and accessing related objects (foriegn keys).
I am developing a form for use both as a HTML form and as part of an offline step running on site (i.e. though a command).
The form is for creating objects (based on ModelForm). The associated model has a foreign key to another model. An instance of this other model needs to be used to validate the object before creation/save.
A field on the model is (more or less) an unique identifier that can be supplied or generated based on the other fields in the model, including the FK object. There is a method on the FK object to ensure that the uid is proper (i.e. fk\_obj.valid\_name(name)).
Is it safe/proper to load the FK object from the form to run this check? This adds a database query to load the object.
WHERE should this be done? Right now Im doing this in the form init function. i.e. \`fkobj = Fk.objects.get(pk=data.get('fk'))\` This doesn't feel right to me, I tried to do it in 'clean' but I still get errors in the form even though Im updating cleaned\_data. Should this be done in full\_clean()?
Also given that I will often need to load this fk object, is it reasonable to pass object to the form, rather than the pk? This will potentially save a lookup, especially when creating many objects at once, which is the most common use case.
Does anyone have a good flow diagram of the lifecycle of a Form object (specifically ModelForm)? It is unclear to me what the flow of a form is supposed to be and what the actual responsibility of each function is supposed to be.
/r/django
https://redd.it/8pueex
I am developing a form for use both as a HTML form and as part of an offline step running on site (i.e. though a command).
The form is for creating objects (based on ModelForm). The associated model has a foreign key to another model. An instance of this other model needs to be used to validate the object before creation/save.
A field on the model is (more or less) an unique identifier that can be supplied or generated based on the other fields in the model, including the FK object. There is a method on the FK object to ensure that the uid is proper (i.e. fk\_obj.valid\_name(name)).
Is it safe/proper to load the FK object from the form to run this check? This adds a database query to load the object.
WHERE should this be done? Right now Im doing this in the form init function. i.e. \`fkobj = Fk.objects.get(pk=data.get('fk'))\` This doesn't feel right to me, I tried to do it in 'clean' but I still get errors in the form even though Im updating cleaned\_data. Should this be done in full\_clean()?
Also given that I will often need to load this fk object, is it reasonable to pass object to the form, rather than the pk? This will potentially save a lookup, especially when creating many objects at once, which is the most common use case.
Does anyone have a good flow diagram of the lifecycle of a Form object (specifically ModelForm)? It is unclear to me what the flow of a form is supposed to be and what the actual responsibility of each function is supposed to be.
/r/django
https://redd.it/8pueex
reddit
r/django - Confused on forms (ModelForms) and accessing related objects (foriegn keys).
3 votes and 3 so far on reddit
[Project] Does popularity of technology on Stack Overflow influence popularity of post about this technology on Hacker News?
[Link to the project](https://github.com/dgwozdz/HN_SO_analysis).
I tried to answer a question whether popularity of a given technology (programming language/framework/library) on Stack Overflow is a cause of popularity of posts with regard to this technology on Hacker News. The project included an analysis of plots of number of questions/points on Stack Overflow and Hacker News (a.k.a. some Exploratory Data Analysis) as well as Granger causality test. It was conducted in Python (\+ a bit of Google BigQuery to get data with regard to Hacker News).
/r/pystats
https://redd.it/8q00qz
[Link to the project](https://github.com/dgwozdz/HN_SO_analysis).
I tried to answer a question whether popularity of a given technology (programming language/framework/library) on Stack Overflow is a cause of popularity of posts with regard to this technology on Hacker News. The project included an analysis of plots of number of questions/points on Stack Overflow and Hacker News (a.k.a. some Exploratory Data Analysis) as well as Granger causality test. It was conducted in Python (\+ a bit of Google BigQuery to get data with regard to Hacker News).
/r/pystats
https://redd.it/8q00qz
GitHub
dgwozdz/HN_SO_analysis
Is there a relationship between popularity of a given technology on Stack Overflow (SO) and Hacker News (HN)? And a few words about causality - dgwozdz/HN_SO_analysis
Is it a bad idea to use .gz files?
Hello everyone. I'm working on my first python project/Flask web app, deploying it to the web some time later, and I was thinking about using a .gz file of a pandas data frame for a very small database containing resource links/type, etc., rather than SQLite db. Is this a bad idea? Should I be sticking to just a SQLite database? Any advice would be great.
/r/flask
https://redd.it/8mxqy2
Hello everyone. I'm working on my first python project/Flask web app, deploying it to the web some time later, and I was thinking about using a .gz file of a pandas data frame for a very small database containing resource links/type, etc., rather than SQLite db. Is this a bad idea? Should I be sticking to just a SQLite database? Any advice would be great.
/r/flask
https://redd.it/8mxqy2
reddit
r/flask - Is it a bad idea to use .gz files?
2 votes and 4 so far on reddit
I made a non-motivational poster generator
I created a non\-motivational (and non\-cool) poster generator. Check it out:
[https://github.com/rubennp91/InstaQuotes](https://github.com/rubennp91/InstaQuotes)
The script takes an instagram profile, a random quote from a wikiquote page and puts it together into what can only be called a non\-motivational poster. Here are some examples:
[https://i.imgur.com/HpN7kgc.png](https://i.imgur.com/HpN7kgc.png) (natgeo \+ Trump)
[https://i.imgur.com/COhxzb9.jpg](https://i.imgur.com/COhxzb9.jpg) (selenagomez \+ Karl Marx)
[https://i.imgur.com/9418KPb.jpg](https://i.imgur.com/9418KPb.jpg) (taylorswift \+ Churchill)
Some can be pretty hilarious.
So this script is not ideal, as it depends on a ton of libraries and even a tool for downloading instagram media, but even so, I had a ton of fun developing it. It uses instagram\-scraper for media downloading (pip install instagram\-scraper), wikiquotes (pip install wikiquotes) to retrieve the quotes, and PIL (pip install pillow) to add the text to the image. I've uploaded a sample font ([https://fonts.google.com/specimen/Indie\+Flower](https://fonts.google.com/specimen/Indie+Flower)) with it on github but more can be added to the fonts folder.
I started programming python some months ago in my job and I'm loving it so far. I only had some experience programming microcontrollers in C and learning python has opened a world of opportunities for me.
The code is thoroughly commented but if you have any questions, please ask ahead.
/r/Python
https://redd.it/8q0802
I created a non\-motivational (and non\-cool) poster generator. Check it out:
[https://github.com/rubennp91/InstaQuotes](https://github.com/rubennp91/InstaQuotes)
The script takes an instagram profile, a random quote from a wikiquote page and puts it together into what can only be called a non\-motivational poster. Here are some examples:
[https://i.imgur.com/HpN7kgc.png](https://i.imgur.com/HpN7kgc.png) (natgeo \+ Trump)
[https://i.imgur.com/COhxzb9.jpg](https://i.imgur.com/COhxzb9.jpg) (selenagomez \+ Karl Marx)
[https://i.imgur.com/9418KPb.jpg](https://i.imgur.com/9418KPb.jpg) (taylorswift \+ Churchill)
Some can be pretty hilarious.
So this script is not ideal, as it depends on a ton of libraries and even a tool for downloading instagram media, but even so, I had a ton of fun developing it. It uses instagram\-scraper for media downloading (pip install instagram\-scraper), wikiquotes (pip install wikiquotes) to retrieve the quotes, and PIL (pip install pillow) to add the text to the image. I've uploaded a sample font ([https://fonts.google.com/specimen/Indie\+Flower](https://fonts.google.com/specimen/Indie+Flower)) with it on github but more can be added to the fonts folder.
I started programming python some months ago in my job and I'm loving it so far. I only had some experience programming microcontrollers in C and learning python has opened a world of opportunities for me.
The code is thoroughly commented but if you have any questions, please ask ahead.
/r/Python
https://redd.it/8q0802
GitHub
rubennp91/InstaQuotes
This is a tool for creating non-motivational posters. Just give it an instagram profile, a wikiquote name to search and a language. - rubennp91/InstaQuotes
Django Friday Tips: Links that maintain the current query params
https://blog.ovalerio.net/archives/1512
/r/djangolearning
https://redd.it/8q2rgl
https://blog.ovalerio.net/archives/1512
/r/djangolearning
https://redd.it/8q2rgl
%5E being inserted into reverse url
Hi everyone,
I'm having a problem with a url resolving as it should. It's resolving as
[http://localhost:8000/%5Ewebsites/?value=1&id=1](http://localhost:8000/%5Ewebsites/?value=1&id=1) when it should be resolving as
[http://localhost:8000/websites/?value=1&id=1](http://localhost:8000/websites/?value=1&id=1)
I've got the following urls.py inside of an app.
app_name = 'websites'
urlpatterns = [
url(r'^$', website_views.homepage, name="homepage"),
url(r'^blog/$', website_views.blog, name="blog"),
url(r'^blog/(?P<id>\d+)/$', website_views.blogpost, name="blogpost"),
I've got this inside of my project urls.py file:
urlpatterns = [
url(r'^websites/', include('websites.customerurls')),
The link that is resolving incorrectly is this:
<a href="{% url 'websites:homepage' %}?value=1&id=1" target="_blank">
Any help would be appreciated! Thanks!
Update. Ok, so I removed the caret from url(r'\^websites/', include('websites.customerurls')),
Result: URL resolves in the same way, but the page now loads without any errors.
I put caret back. URL now resolves as desired and page loads. I think I just came across a bit of a bug or something. I'm glad that this worked out, because I feel like I have a decent understanding of django and this made 0 sense. Thanks all for your help!
/r/django
https://redd.it/8q3cul
Hi everyone,
I'm having a problem with a url resolving as it should. It's resolving as
[http://localhost:8000/%5Ewebsites/?value=1&id=1](http://localhost:8000/%5Ewebsites/?value=1&id=1) when it should be resolving as
[http://localhost:8000/websites/?value=1&id=1](http://localhost:8000/websites/?value=1&id=1)
I've got the following urls.py inside of an app.
app_name = 'websites'
urlpatterns = [
url(r'^$', website_views.homepage, name="homepage"),
url(r'^blog/$', website_views.blog, name="blog"),
url(r'^blog/(?P<id>\d+)/$', website_views.blogpost, name="blogpost"),
I've got this inside of my project urls.py file:
urlpatterns = [
url(r'^websites/', include('websites.customerurls')),
The link that is resolving incorrectly is this:
<a href="{% url 'websites:homepage' %}?value=1&id=1" target="_blank">
Any help would be appreciated! Thanks!
Update. Ok, so I removed the caret from url(r'\^websites/', include('websites.customerurls')),
Result: URL resolves in the same way, but the page now loads without any errors.
I put caret back. URL now resolves as desired and page loads. I think I just came across a bit of a bug or something. I'm glad that this worked out, because I feel like I have a decent understanding of django and this made 0 sense. Thanks all for your help!
/r/django
https://redd.it/8q3cul
reddit
%5E being inserted into reverse url • r/django
Hi everyone, I'm having a problem with a url resolving as it should. It's resolving...
I made a tool to retrieve your youtube subscriptions or analyze them.
https://gitlab.com/sawyerf/Youtube_subscription_manager
/r/Python
https://redd.it/8q2kwv
https://gitlab.com/sawyerf/Youtube_subscription_manager
/r/Python
https://redd.it/8q2kwv
GitLab
Sawyerf / Youtube_subscription_manager
An alternative to youtube.com to retrieve the videos of your subscriptions feed without requires an account
Build a voice interface in three minutes with PORCUPiNE
https://www.youtube.com/watch?v=3z7LBW_Rl9c
/r/Python
https://redd.it/8q2kqj
https://www.youtube.com/watch?v=3z7LBW_Rl9c
/r/Python
https://redd.it/8q2kqj
YouTube
Porcupine Tutorial - Wake Word
How to activate any device using voice without a cloud connection http://picovoice.ai/ https://github.com/Picovoice/Porcupine
How useful would a database that a contains the dependencies of all pip packages be? Would this be a good tool that would help developers check the dependencies of a package before installing it?
/r/Python
https://redd.it/8q3cye
/r/Python
https://redd.it/8q3cye
reddit
r/Python - How useful would a database that a contains the dependencies of all pip packages be? Would this be a good tool that…
7 votes and 12 so far on reddit
Small TicTacToe implementation to learn bitwise operations
https://github.com/gabrielchiconi/bloquinhos-py/blob/master/jogo_da_velha/bitwise.py
/r/Python
https://redd.it/8q69hr
https://github.com/gabrielchiconi/bloquinhos-py/blob/master/jogo_da_velha/bitwise.py
/r/Python
https://redd.it/8q69hr
GitHub
gabrielchiconi/bloquinhos-py
bloquinhos-py - Bloquinhos de código e programetes em Python
Making an NLP application in python
I need to make an NLP tool for an IoT application that will basically take queries from users (like 'turn on the pump for 1 minute and inform Mark') and will process it to extract 3 basic things for further processing \- the device (pump), duration (1 minute) and the person (Mark). How should I proceed with this?
/r/Python
https://redd.it/8q7c7c
I need to make an NLP tool for an IoT application that will basically take queries from users (like 'turn on the pump for 1 minute and inform Mark') and will process it to extract 3 basic things for further processing \- the device (pump), duration (1 minute) and the person (Mark). How should I proceed with this?
/r/Python
https://redd.it/8q7c7c
reddit
r/Python - Making an NLP application in python
2 votes and 0 so far on reddit
Help with quick app - what should I use?
So I am wanting to make a private, sort of quick hobbyist web app using python to gather data from Monopoly games. It definitely does not have to have many features but I would like it to have a mainly visual interface (not command line / text\-based) and do enough that it does not take away from playing Monopoly. My desire is for the player\-facing web app to be essentially like a VERY small Venmo for monopoly where players can keep track of their balances and send money to each other with a short memo attached. Additionally, I would like it to keep track of owned properties and property upgrades with the ability to trade them with other players and keep track of the amount that the properties were traded for. On the backend, I would like to keep track of all actions into a SQLite database for later analysis of the transactions themselves. The application would be a web app that *each* player can access from their phones (not an app downloaded from an app store), and mostly single view with a couple pop\-up dialogue., Maybe a settings page if we are getting frisky with the features. \-\-I really only say all this to describe the (shallow) depth of this project and what I would need it to do. This definitely is not a 'scalability' or 'big data' problem.
In the past i have used Google Sheets to keep track of data and had my players learned on my preferred format, but it really made the game not fun to actually play (which inevitably affects the data itself). **I am hoping for guidance with finding a small stack of packages/technologies in Python I could use quickly to build this app, and hopefully have it remain free to develop** (server hosted locally, spun up for each monopoly game is an idea). The app's main purpose is to gather data, not be a robust tool to use with every Monopoly game (maybe eventually...), but still be visual enough for the players and not detract from gameplay. I have been needing to learn Flask/Django and this could be my opportunity but would they be too much for this project? What would be the *easiest* stack with the smallest learning curve?
Thanks all for the input!
/r/Python
https://redd.it/8q7mdr
So I am wanting to make a private, sort of quick hobbyist web app using python to gather data from Monopoly games. It definitely does not have to have many features but I would like it to have a mainly visual interface (not command line / text\-based) and do enough that it does not take away from playing Monopoly. My desire is for the player\-facing web app to be essentially like a VERY small Venmo for monopoly where players can keep track of their balances and send money to each other with a short memo attached. Additionally, I would like it to keep track of owned properties and property upgrades with the ability to trade them with other players and keep track of the amount that the properties were traded for. On the backend, I would like to keep track of all actions into a SQLite database for later analysis of the transactions themselves. The application would be a web app that *each* player can access from their phones (not an app downloaded from an app store), and mostly single view with a couple pop\-up dialogue., Maybe a settings page if we are getting frisky with the features. \-\-I really only say all this to describe the (shallow) depth of this project and what I would need it to do. This definitely is not a 'scalability' or 'big data' problem.
In the past i have used Google Sheets to keep track of data and had my players learned on my preferred format, but it really made the game not fun to actually play (which inevitably affects the data itself). **I am hoping for guidance with finding a small stack of packages/technologies in Python I could use quickly to build this app, and hopefully have it remain free to develop** (server hosted locally, spun up for each monopoly game is an idea). The app's main purpose is to gather data, not be a robust tool to use with every Monopoly game (maybe eventually...), but still be visual enough for the players and not detract from gameplay. I have been needing to learn Flask/Django and this could be my opportunity but would they be too much for this project? What would be the *easiest* stack with the smallest learning curve?
Thanks all for the input!
/r/Python
https://redd.it/8q7mdr
reddit
Help with quick app - what should I use? • r/Python
So I am wanting to make a private, sort of quick hobbyist web app using python to gather data from Monopoly games. It definitely does not have to...
Django REST and OAuth2
Hello everyone!
I'm starting a project with *Django REST framework* , and I have a question about the login operation, I'm planning to use OAuth2.
When I get the user information, login, password, etc, and I use a serialize, all that information is going to a json api, but what about the password? It will be there in the middle of the json for everyone (I mean everyone who is working in the project) to see it? I'm just wondering, I didn't start the project yet, about to start this next week.
/r/django
https://redd.it/8q5xvf
Hello everyone!
I'm starting a project with *Django REST framework* , and I have a question about the login operation, I'm planning to use OAuth2.
When I get the user information, login, password, etc, and I use a serialize, all that information is going to a json api, but what about the password? It will be there in the middle of the json for everyone (I mean everyone who is working in the project) to see it? I'm just wondering, I didn't start the project yet, about to start this next week.
/r/django
https://redd.it/8q5xvf
reddit
r/django - Django REST and OAuth2
4 votes and 2 so far on reddit
Forms: Attribute Error
As part of learning Django, I am currently trying to build a little project. At this stage, as a preliminary task, I am attempting to pass the user\-input from the Form to the template (results.html). However, I am getting the error the below, and I can't seem figure out why!
If anybody could let me know what I'm doing wrong, I'd be greatly appreciative :D Thanks in advance!
**ERROR**
AttributeError at /search_query/ 'SearchHashtagForm' object has no attribute 'cleaned_data'
Request Method: POST
Django Version: 2.0
Exception Type: AttributeError
Exception Value:'SearchHashtagForm' object has no attribute 'cleaned_data'
Exception Location:/mnt/data/.python-3.6/lib/python3.6/site-packages/django/forms/models.py in _save_m2m, line 424
Python Executable:/mnt/data/.python-3.6/bin/python
Python Version:3.6.5
Python Path:['/mnt/project', '/mnt/data/.python-3.6/lib/python36.zip', '/mnt/data/.python-3.6/lib/python3.6', '/mnt/data/.python-3.6/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6', '/mnt/data/.python-3.6/lib/python3.6/site-packages']
**models.py**
from django.db import models
class Location(models.Model):
""" Model representing a Location, attached to Hashtag objects through a M2M
relationship """
name = models.CharField(max_length=1400)
def __str__(self):
return self.name
class Hashtag(models.Model):
""" Model representing a specific Hashtag serch by user """
search_text = models.CharField(max_length=140, primary_key=True)
locations = models.ManyToManyField(Location, blank=True)
def __str__(self):
""" String for representing the Model object (search_text) """
return self.search_text
def display_locations(self):
""" Creates a list of the locations """
# Return a list of location names attached to the Hashtag model
return self.locations.values_list('name', flat=True).all()
**forms.py**
from django import forms
from django.forms import ModelForm
from .models import Location, Hashtag
class SearchHashtagForm(ModelForm):
""" ModelForm for user to search by hashtag """
def clean_hashtag(self):
data = self.cleaned_data['search_text']
# Check search_query doesn't include '#'. If so, remove it
if data[0] == '#':
data = data[1:]
# return the cleaned data
return data
class Meta:
model = Hashtag
fields = ['search_text',]
labels = {'search_text':('Hashtag Search'), }
help_texts = { 'search_text': ('Enter a hashtag to search.'), }
**views.py**
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from .models import Location, Hashtag
from .forms import SearchHashtagForm
def hashtag_search_index(request):
""" View function for user to enter search query """
hashtag_search = SearchHashtagForm()
# If POST, process Form data
if request.method == 'POST':
# Create a form instance and populate it with data from request (binding)
form = SearchHashtagForm(request.POST)
# Check if form is valid
if form.is_valid():
hashtag_search.search_text = form.cleaned_data['search_text']
hashtag_search.save()
# redirect to a new URL
return HttpResponseRedirect(reverse('mapping_twitter:results'))
# If GET (or any other method), create the default form
else:
form = SearchHashtagForm()
context = {'form':form}
return render(request, 'mapping_twitter/hashtag_search_index.html', context)
def results(request):
""" View for
As part of learning Django, I am currently trying to build a little project. At this stage, as a preliminary task, I am attempting to pass the user\-input from the Form to the template (results.html). However, I am getting the error the below, and I can't seem figure out why!
If anybody could let me know what I'm doing wrong, I'd be greatly appreciative :D Thanks in advance!
**ERROR**
AttributeError at /search_query/ 'SearchHashtagForm' object has no attribute 'cleaned_data'
Request Method: POST
Django Version: 2.0
Exception Type: AttributeError
Exception Value:'SearchHashtagForm' object has no attribute 'cleaned_data'
Exception Location:/mnt/data/.python-3.6/lib/python3.6/site-packages/django/forms/models.py in _save_m2m, line 424
Python Executable:/mnt/data/.python-3.6/bin/python
Python Version:3.6.5
Python Path:['/mnt/project', '/mnt/data/.python-3.6/lib/python36.zip', '/mnt/data/.python-3.6/lib/python3.6', '/mnt/data/.python-3.6/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6', '/mnt/data/.python-3.6/lib/python3.6/site-packages']
**models.py**
from django.db import models
class Location(models.Model):
""" Model representing a Location, attached to Hashtag objects through a M2M
relationship """
name = models.CharField(max_length=1400)
def __str__(self):
return self.name
class Hashtag(models.Model):
""" Model representing a specific Hashtag serch by user """
search_text = models.CharField(max_length=140, primary_key=True)
locations = models.ManyToManyField(Location, blank=True)
def __str__(self):
""" String for representing the Model object (search_text) """
return self.search_text
def display_locations(self):
""" Creates a list of the locations """
# Return a list of location names attached to the Hashtag model
return self.locations.values_list('name', flat=True).all()
**forms.py**
from django import forms
from django.forms import ModelForm
from .models import Location, Hashtag
class SearchHashtagForm(ModelForm):
""" ModelForm for user to search by hashtag """
def clean_hashtag(self):
data = self.cleaned_data['search_text']
# Check search_query doesn't include '#'. If so, remove it
if data[0] == '#':
data = data[1:]
# return the cleaned data
return data
class Meta:
model = Hashtag
fields = ['search_text',]
labels = {'search_text':('Hashtag Search'), }
help_texts = { 'search_text': ('Enter a hashtag to search.'), }
**views.py**
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from .models import Location, Hashtag
from .forms import SearchHashtagForm
def hashtag_search_index(request):
""" View function for user to enter search query """
hashtag_search = SearchHashtagForm()
# If POST, process Form data
if request.method == 'POST':
# Create a form instance and populate it with data from request (binding)
form = SearchHashtagForm(request.POST)
# Check if form is valid
if form.is_valid():
hashtag_search.search_text = form.cleaned_data['search_text']
hashtag_search.save()
# redirect to a new URL
return HttpResponseRedirect(reverse('mapping_twitter:results'))
# If GET (or any other method), create the default form
else:
form = SearchHashtagForm()
context = {'form':form}
return render(request, 'mapping_twitter/hashtag_search_index.html', context)
def results(request):
""" View for