using a custom module inside django app?
First I wanted to say that for some reason, this sub capitalizes the first letter of every word in the title. I swear lol..
Note: I'm using Django 1.10
I'm attempting to build my first web app using Django and I'm running into some issues. The overview of the project is that I will have a worker thread that gathers information from somewhere and adds it to the database (preferably using the Django models) and I will display this information on the webpage.
Currently I have my script imported in the settings.py and I call it in the ready() like so:
import bot #my custom module
class myapp(AppConfig):
def ready(self):
thread = Thread(target=bot.run, kwargs={"test" : True})
thread.start()
But the issue is that I don't know how to access my models and my database from inside that my custom module in order to save the data is gathers. Is there another approach I can take or is this not possible?
EDIT: Found a solution. [Check comments](https://www.reddit.com/r/djangolearning/comments/63rc8w/using_a_custom_module_inside_django_app/dfx0xjf/).
/r/djangolearning
https://redd.it/63rc8w
First I wanted to say that for some reason, this sub capitalizes the first letter of every word in the title. I swear lol..
Note: I'm using Django 1.10
I'm attempting to build my first web app using Django and I'm running into some issues. The overview of the project is that I will have a worker thread that gathers information from somewhere and adds it to the database (preferably using the Django models) and I will display this information on the webpage.
Currently I have my script imported in the settings.py and I call it in the ready() like so:
import bot #my custom module
class myapp(AppConfig):
def ready(self):
thread = Thread(target=bot.run, kwargs={"test" : True})
thread.start()
But the issue is that I don't know how to access my models and my database from inside that my custom module in order to save the data is gathers. Is there another approach I can take or is this not possible?
EDIT: Found a solution. [Check comments](https://www.reddit.com/r/djangolearning/comments/63rc8w/using_a_custom_module_inside_django_app/dfx0xjf/).
/r/djangolearning
https://redd.it/63rc8w
reddit
using a custom module inside django app? • r/djangolearning
First I wanted to say that for some reason, this sub capitalizes the first letter of every word in the title. I swear lol.. Note: I'm using...
My first Python program :) (I'm hooked)
So, Python was on my list of things to teach myself after I took a C++ course this spring (I regret that, should have taken Python or Java).
So, I am proud of this stupid little dice roller because I evolved it from a basic "This rolls two dice" to "How many do you want to roll?" with input validation. Yay me.
#my first Python program.
#I chose this one to compare to a similar program made
in C++
import random
import time
roll = "yes"
numDice = 0
print("This program will roll as many dice as you need.")
time.sleep(1) #this makes the program pause a bit
print("\nWould you like to roll?")
roll = input()
#input validation to make sure they type yes (y) or no
(no)
while roll != "yes" and roll != "y" and roll != "no" and roll
!= "n":
print("\nType yes (or 'y') or no (or 'n') to roll.")
roll = input()
while roll == "yes" or roll == "y":
print("\nHow many dice do you want to roll?")
numDice = int(input())
print("\nRolling...")
time.sleep(2) #again, a pause
print("\nYou got: ")
for i in range(0, numDice): #this iterates as many times
as the user wants
print(random.randint(1,6)) #generates the random
numbers between 1-6
print("\nRoll again?")
roll = input()
#input validation again
while roll != "yes" and roll != "y" and roll != "no" and
roll != "n":
print("\nType yes or no.")
roll = input()
print("\nThanks for using my program!")
input()
/r/Python
https://redd.it/6hmq58
So, Python was on my list of things to teach myself after I took a C++ course this spring (I regret that, should have taken Python or Java).
So, I am proud of this stupid little dice roller because I evolved it from a basic "This rolls two dice" to "How many do you want to roll?" with input validation. Yay me.
#my first Python program.
#I chose this one to compare to a similar program made
in C++
import random
import time
roll = "yes"
numDice = 0
print("This program will roll as many dice as you need.")
time.sleep(1) #this makes the program pause a bit
print("\nWould you like to roll?")
roll = input()
#input validation to make sure they type yes (y) or no
(no)
while roll != "yes" and roll != "y" and roll != "no" and roll
!= "n":
print("\nType yes (or 'y') or no (or 'n') to roll.")
roll = input()
while roll == "yes" or roll == "y":
print("\nHow many dice do you want to roll?")
numDice = int(input())
print("\nRolling...")
time.sleep(2) #again, a pause
print("\nYou got: ")
for i in range(0, numDice): #this iterates as many times
as the user wants
print(random.randint(1,6)) #generates the random
numbers between 1-6
print("\nRoll again?")
roll = input()
#input validation again
while roll != "yes" and roll != "y" and roll != "no" and
roll != "n":
print("\nType yes or no.")
roll = input()
print("\nThanks for using my program!")
input()
/r/Python
https://redd.it/6hmq58
reddit
My first Python program :) (I'm hooked) • r/Python
So, Python was on my list of things to teach myself after I took a C++ course this spring (I regret that, should have taken Python or Java). So,...
ction will return an exception and your program will stop/crash**. You need to take care of these situations.
This is achieved using `try`, `except` and `else` blocks. [See here](https://docs.python.org/3/tutorial/errors.html) Here is the code for these 2 steps-
file_obj_r = open(path,'r')
try:
explanation = fetchdata(myurl)
except:
print('Exception!!! Possibly incorrect xkcd URL...\n')
# Typical cause for this will be a URL for an xkcd that does not exist (Example: https://www.xkcd.com/772524318/)
else:
if comment.id not in file_obj_r.read().splitlines():
print('Link is unique...posting explanation\n')
comment.reply(header + explanation + footer)
file_obj_r.close()
file_obj_w = open(path,'a+')
file_obj_w.write(comment.id + '\n')
file_obj_w.close()
else:
print('Already visited link...No reply needed\n')
Notice how I make a reply to a relevant comment by using the variables from Section 1 to print header and footer description alongside the explanation - `comment.reply(header + explanation + footer)`
Next we make some `sleep` statements to stop the bot from querying Reddit too fast. If the Reddit API returns an error due to too many requests, adjust `val` in the instances of `time.sleep(val)` in your program.
##Section 5: The main function
https://gist.github.com/aydwi/99323ebd710428f4590077a844236f83
We are almost done here. We wrap our functions into a `main` function by calling `authenticate()` (remember Section 1) and passing it to the function which runs the bot, namely `run_explainbot(reddit)`. Since we are calling it inside `while` loop with the expression 'True', it will run indefinitely.
#My bot in action
Test post to show the bot in action: https://redd.it/6tey71 (Since I'm not running it continuously as of now, it won't reply to every comment containing an xkcd link there)
My Terminal emulator while running the bot: http://imgur.com/4TzEyor
**Finally, if you want to take this project further, you are welcome to contribute code to [my Github repository](https://github.com/aydwi/explainxkcdbot). You can fork it (please note that there are still some checks to be added in order to make the bot more Reddit friendly), star it for future or open a pull request.** I'll try to add more details about file handling and scraping aspects of the program once I get some time.
**THANK YOU ALL**
/r/Python
https://redd.it/78uvdw
This is achieved using `try`, `except` and `else` blocks. [See here](https://docs.python.org/3/tutorial/errors.html) Here is the code for these 2 steps-
file_obj_r = open(path,'r')
try:
explanation = fetchdata(myurl)
except:
print('Exception!!! Possibly incorrect xkcd URL...\n')
# Typical cause for this will be a URL for an xkcd that does not exist (Example: https://www.xkcd.com/772524318/)
else:
if comment.id not in file_obj_r.read().splitlines():
print('Link is unique...posting explanation\n')
comment.reply(header + explanation + footer)
file_obj_r.close()
file_obj_w = open(path,'a+')
file_obj_w.write(comment.id + '\n')
file_obj_w.close()
else:
print('Already visited link...No reply needed\n')
Notice how I make a reply to a relevant comment by using the variables from Section 1 to print header and footer description alongside the explanation - `comment.reply(header + explanation + footer)`
Next we make some `sleep` statements to stop the bot from querying Reddit too fast. If the Reddit API returns an error due to too many requests, adjust `val` in the instances of `time.sleep(val)` in your program.
##Section 5: The main function
https://gist.github.com/aydwi/99323ebd710428f4590077a844236f83
We are almost done here. We wrap our functions into a `main` function by calling `authenticate()` (remember Section 1) and passing it to the function which runs the bot, namely `run_explainbot(reddit)`. Since we are calling it inside `while` loop with the expression 'True', it will run indefinitely.
#My bot in action
Test post to show the bot in action: https://redd.it/6tey71 (Since I'm not running it continuously as of now, it won't reply to every comment containing an xkcd link there)
My Terminal emulator while running the bot: http://imgur.com/4TzEyor
**Finally, if you want to take this project further, you are welcome to contribute code to [my Github repository](https://github.com/aydwi/explainxkcdbot). You can fork it (please note that there are still some checks to be added in order to make the bot more Reddit friendly), star it for future or open a pull request.** I'll try to add more details about file handling and scraping aspects of the program once I get some time.
**THANK YOU ALL**
/r/Python
https://redd.it/78uvdw