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
Django template inclusion issue

Hi,

being lost how include a template without that annoying newline added.

For example, separated and generalized logic for generating attributes for a html tag:

# templates/widgets/attrs.html
{% for name, value in attrs.items %} {{ name }}="{{ value|stringformat:'s' }}"{% endfor %}


When included in a parent template,

# widgets/list_widget_item.html
<li{% include "widgets/attrs.html" %}>List Item Element</li>

django renders it with unwanted newline

<li class="my-li-class"
>List Item Element</li>


Although valid and handled properly by most browsers, I'd like to get expected result instead of that compromise.

I've tried enclose inclusion with `spaceless` tag, however it removes *all* whitespace characters including (optional) leading space, not only the newline.

Any better solution to this straight and trivial demand ?


/r/django
https://redd.it/7fnv8c
Django too complicated for someone like me?

I've been trying to create some sort of political quiz for a long, long while now.

I have little programming knowledge, although I've been using Python in not much depth for the past year.

My progress has been awfully slow, I haven't found the tutorials to be helpful at all and nobody else is really helping me in any depth... is it worth just giving up for now and returning to Django when I have more knowledge of Python?

All I've been trying to do is make a quiz, which has about 50 questions, all which have the options "Strongly Agree" to "Strongly Disagree" with a Neutral option. I wanted the result to be stored in some sort of database so they could be interpreted and a result be generated at the end.

I have gone from using two separate models like the Django tutorial have (Questions and Choices) to just one (Questions) that has the options hard coded into it... but I haven't been able to get those to show up anywhere (things jinja templating are completely lost on me)

/r/djangolearning
https://redd.it/7fesgx
is "Flask Web Development: Developing Web Applications with Python 1st edition" still readable or is too outdated?

this is the link for it on amazon,

https://www.amazon.com/Flask-Web-Development-Developing-Applications/dp/1449372627

I have it in print but don't know if it's worth the effort?

/r/flask
https://redd.it/7fq6es
When would I use Docker?

I’m currently work on 2 apps on my windows computer, using virtualenv and PostgreSQL. At some point I plan on deploying to the web. Where, I’m not sure. It might end up being my company’s webserver (I need web hosting in Canada which narrows the choices).

I just learned about Docker and I’m wondering if this type of tool is something I should start learning about and using? I’m not 100% sure of what Docker does so I’m just trying to figure out if it’s something I need to spend time on. Certainly the little bit I know makes it appealing: develop in a Docker container and then I’d be able to (more easily?) deploy my container and app to whatever webserver I choose.

/r/django
https://redd.it/7fokfz
[R] StarGAN: Unified Generative Adversarial Networks for Multi-Domain Image-to-Image Translation

/r/MachineLearning
https://redd.it/7fro3g
Python Guess The Number Game

Just made my first real app in python and was wondering if anyone wanted to give some feedback on it. All is appreciated!

Code:

import random

# Prints credits
print("\t\t\tGuess The Number")

print("\t\t\t \\ \\ \\ \\ \\ \\ \\ \\")
print("\t\t\t\tby")
print("\t\t\t Brook Jeynes")
print("\t\t\t \\ \\ \\ \\ \\ \\")

# Setting values
number_of_guesses = 3
computer_choice = random.randint(1,10)

# Testing number choice
while number_of_guesses != 0:
user_choice = int(input("\nI'm thinking of a number
between 1 and 10:\n"))
# If you guess the number print VVV
if user_choice == computer_choice:
print("Congratulations, you guessed my number")
print("My number was", computer_choice)
number_of_guesses = number_of_guesses - 1
print("You had", number_of_guesses, "guesses left")
number_of_guesses = number_of_guesses + 1
break
# If you didn't guess the number print VVV
elif user_choice != computer_choice:
print("You did not guess my number:\n")
if user_choice > computer_choice:
print("Your guess is too high. Guess lower...")
elif user_choice < computer_choice:
print("Your guess is too low. Guess higher...")
number_of_guesses = number_of_guesses - 1
print("You have", number_of_guesses, "guesses
left")

# Test number of guesses
if number_of_guesses == 0:
print("""
_______ _______ _______ _______ _______ _______ _______
( ____ \( ___ )( )( ____ \ ( ___ )|\ /|( ____ \( ____ )
| ( \/| ( ) || () () || ( \/ | ( ) || ) ( || ( \/| ( )|
| | | (___) || || || || (__ | | | || | | || (__ | (____)|
| | ____ | ___ || |(_)| || __) | | | |( ( ) )| __) | __)
| | \_ )| ( ) || | | || ( | | | | \ \_/ / | ( | (\ (
| (___) || ) ( || ) ( || (____/\ | (___) | \ / | (____/\| ) \ \__
(_______)|/ \||/ \|(_______/ (_______) \_/ (_______/|/ \__/
""")
print("You did not guess my number")
print("My number was", computer_choice)



/r/Python
https://redd.it/7ftn3c
Need help with passing href parameters!

So I'm having trouble with passing an argument through href. I'm using twitter api and trying to return info on users, but it's returning nothing. What am I doing wrong in the view?

base.html:

<a href="{% url 'users' get_user=search.user.screen_name %}" class="user-profile" name="user">

urls.py:

from django.conf.urls import url, include
from . import views

urlpatterns = [
url(r'^$', views.index, name='views'),
url(r'^user/(?P<get_user>\w+)', views.user, name='users'),

views.py:

def index(request):

if request.GET.get('get_user', None):
user_search = request.GET.get('get_user', None)
get_user = api.GetUser(user_id=None, screen_name=user_search)

return render(request, 'scrape/user/users.html', {'get_user': get_user})

trends = api.GetTrendsCurrent(exclude=None)
searches = api.GetSearch(term='moments', raw_query=None, geocode=None, since_id=None,
max_id=None, until=None, since=None, count=30,
lang=None, locale=None, result_type="popular", include_entities=None)

return render(request, 'scrape/base.html', {'trends': trends, 'searches': searches})

def user(request, get_user):

return render(request, 'scrape/user/users.html', {'get_user': get_user})

/r/django
https://redd.it/7fsm87
What are the benefits of using Django to create a website as opposed to using something like WordPress?



/r/djangolearning
https://redd.it/7fpmcp
A general query about django / javascript patterns and how to best organize/encapsulate

I am currently coding webapps in django and using a bit of javascript to do this and that. Naturally, I sometimes want my javascript apps from knowing the context of my Django template, so the way I've been doing it is hardcoding the js into the .html file.

Example

<script>
var my_var = {{ my_var_from_context }}

Clearly this is ugly as all hell as it forces me to keep my javascript code inside of the .html file. I'd like to make my .js file separate and load it.

Clearly thee are some options like write a function that takes arguments that can be captured in the template then passed.

e.g.

var my_var = {{ my_django_var }}
myFunctionFromScript(my_var)

Is this the best way to be doing this? Is there a better pattern? What are others doing?

Thanks for your help.

-Joe

/r/django
https://redd.it/7fwzqk
Paypal, Django, Local Bank, Integration

I'm thinking about making a web system where people can send me money through their local bank account for buying an online item and I'll buy them that item using my paypal account. So this would be 2 step process. At first they will send me money through their local account and then I'll convert that money into paypal dollars and use those dollars to buy that item for them. I want to know is that possible and if it is, then from where should I start?

/r/djangolearning
https://redd.it/7etabj
Best API for scheduled tasks currently?

I need to set up automatic scheduled tasks. The tasks are pretty simple, like set `is_active_subscriber` to `False` when a user's subscription ends and auto-renewal is off.

I'm aware of cron, but I see that it hasn't been updated for a few years. For simple scheduled tasks, what API is the best option right now?

Thanks.

P.S. I'm building a MVP so I'll be expecting only a small amount of traffic when I first go live. So whatever method I end up using, it should be sufficiently scalable.



/r/django
https://redd.it/7fzled
I'm releasing a free code for the "Automate the Boring Stuff with Python" Udemy course

Use this link to sign up for the "Automate the Boring Stuff with Python" Udemy online course: https://www.udemy.com/automate/?couponCode=PY_ALL_THE_THINGS

It's free until the end of Friday, Dec 1, 2017. Afterwards it goes back to its normal $50 price. (Though you can use this link https://www.udemy.com/automate/?couponCode=FOR_LIKE_10_BUCKS to buy it for $10. And it's an open secret that if you browse Udemy in privacy mode, they'll show you the discount price to lure in a "new" customer. But course creators get a much larger cut when people use their referral codes.)

The course follows the book of the same name, which is available for free, in full, at https://automatetheboringstuff.com under a Creative Commons license. ([Which I encourage you to use to share your own creative works.](https://creativecommons.org/licenses/))

The course is 50 videos and made for people with no previous programming experience. The first 15 videos are free to view on YouTube: https://www.youtube.com/watch?v=1F_OgqRuSdI&list=PL0-84-yl1fUnRuXGFe_F7qSH1LEnn9LkW

And now I will go self-flagellate to atone for my part in legitimizing "cyber monday".

/r/Python
https://redd.it/7fxp46
Free Python Games - Basic Python Games for Beginners
http://www.grantjenks.com/docs/freegames/

/r/Python
https://redd.it/7fzu88