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
Help: Want to build a level exchange site for my video game

I originally posted this question over in [r/gamedev](https://www.reddit.com/r/gamedev/comments/8gj4hq/help_want_to_build_level_exchange_website_for_my/), but I wanted to ask you guys here if you could help point me in the right directions.

I'm pretty comfortable with Python, but I haven't done much with Django, so I'm wondering if there are pre-existing packages that can get me most of the way to where I want to go. I have browsed through the packages myself, but not having worked with Django much, it's difficult to tell how exactly to go about things. I'm just looking for some guidance; any tips are much appreciated.

---

So my game has a level editor, allowing users to make and save level files. I'd like to have a website where people can upload level files (json files under 100kb) to easily share with the community. I don't plan on building an in-game system for sharing levels, because it seems more complex (I'm using GameMaker Studio 1.4).

I'm looking to build something similar to this: http://atlas.dustforce.com

---

At a minimum, I would like users to be able to create accounts, upload level files, browse levels, rate levels, and comment on levels.

In addition, it would be nice if the user could upload an image along with the level (like you see with the dustforce maps. It would also be nice to be able to tag the levels.

/r/django
https://redd.it/8gjavm
Book suggestion - apologies if misplaced

I have been coding at one level or another for 35 years. Z80 assembly to BASIC to (blah blah blah). I've also done a bunch of PHP/JScript, etc. and manage multiple Apache servers.

Okay, all that said, I'm really struggling to find a book that works for me for Django. Walking through virtual environments and Hello World and the "don't worry about what this means for now" process is killing me. I'm sure there are some great books out there that approach Django differently - from a more relatable perspective (to me), but I sure haven't found any.

Not that I'm giving up, just feel that if I could find a slightly different path forward I could accelerate what I'm doing. Are there any suggestions out there for books that can approach from that kind of angle?

Once again, sorry if this is really not the place for the question. I do learn better with 'books' (including ebooks) over web pages and video guides. The challenge with being self-taught is that the only teacher you get is yourself, I guess.

/r/djangolearning
https://redd.it/8gl4ny
Help test Jupyter Notebook 5.5 RC1

There's a pre-release for the next version of notebook out, and we want your help to check for regressions before doing a proper release. You can upgrade to the release candidate by running:

pip install --upgrade --pre notebook

See what's changed: http://jupyter-notebook.readthedocs.io/en/latest/changelog.html#release-5-5-0

/r/IPython
https://redd.it/8gi45z
Serializers and User Profiles - Questions

Hi guys. Please excuse any ignorance, I'm quite new to django and python.

I'm currently working with django rest framework and serializers and I'm confused about a few things.

I am have added 2x profiles to the user model at the moment, for the sake of this post we will just call them facebook and twitter. I have added signals so these profiles get created and updated when users do. Here's the code for that to give you a better idea of things:

@receiver(post_save, sender=User)
def user_created_or_saved_signal(sender, instance, created, **kwargs):
"""
Hook the User models create and save signals
"""
if created:
Facebook.objects.create(user=instance)
Twitter.objects.create(user=instance)
instance.Facebook.save()
instance.Twitter.save()

Now I'm working through the DRF docs for adding serializers and have came to a section about nested objects and how to deal with them. (http://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations)

It speaks about writting `create` and `update` serializer functions which also create and update my profiles. My thoughts are I shouldn't have to do this since I already have the signals in place. But now I am also thinking if this will even work properly with signals and if there are any implications to doing things like this.

If what I have so far is OK and I do still have to write those `create` and `update` serializer methods - Why do I have to? Why don't the signals work?

Also when are these serializer methods even used? Are they used in my views? For example:

class SnippetList(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer

def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)

Will `get` use the `list` serializer function and `post` use the `create` serialize r function?

Thanks a lot for any help and suggestions

/r/django
https://redd.it/8gorja
Not getting the context data of dynamically-generated formset

Not getting the context data of dynamically\-generated formset

I've created a CreateView with a Parent form with multiple Children formsets created via Javascript, but I cannot access to the data of the form when I'm creating more than the extra value formsets:

\- If 'extra=1', I can create more than 1, but won't save any. If I just create 1, it will save it.

\- If 'extra=3', I can create more than 3, but won't save any. If I just create 3, it will save them.

I've looking for what to do without sucess until this morning, when I've noticed that the problem it's I'm not getting the 'value' of every field properly. I've printed the get\_context\_data\['child\_form'\] when I'm creating 3 child forms dynamically with extra=1 \(left\) and when I just have extra=3 and I don't create more dynamically \(right\) [https://www.diffchecker.com/XskfB9y3](https://www.diffchecker.com/XskfB9y3)

Seems like I'm not getting the values and I don't know why, so I'll be very happy if anyone can point out what I'm missing. Here's the relevant code:

[forms.py](https://forms.py)

class ParentForm(ModelForm):
class Meta:
model = Parent
exclude = ()

class ChildForm(ModelForm):
class Meta:
model = Child
fields = ....

ChildFormSet = inlineformset_factory(Parent, Child, form=ChildForm, can_delete=True, extra=1)

[views.py](https://views.py)

class ParentCreateView(LoginRequiredMixin, CreateView):
model = Entrada
fields = ...

def get_context_data(self, **kwargs):
data = super(ParentCreateView, self).get_context_data(**kwargs)
if self.request.POST:
data['child_form'] = ChildFormSet(self.request.POST)
data['materials'] = Material.objects.all()
else:
data['child_form'] = ChildFormSet()
data['materials'] = Material.objects.all()
return data

def form_valid(self, form):
context = self.get_context_data()
child_form = context['child_form']
with transaction.atomic():
self.object = form.save()
if child_form.is_valid():
child_form.instance = self.object
child_form.field1 = self.object.id
child_form.save()
return super(ParentCreateView, self).form_valid(form)

template.html

<form class="own-form" action="" method="post">
{% csrf_token %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
<h2 class="text-center text-header"> New Entry</h2>

<div class="form-group">
{% for field in form.visible_fields %}
<div class="form-group row">
<div class="col-4 text-center">{{ field.label_tag }}</div>
<div class="col-8">{{ field }}</div>
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
</div>
<hr>

<div class="form-group form-material-box row form-0">
<div class="col-3 text-center">
<label>Total weight: </label>
<input type="number" id="kg_0">
</div>
<div class="col-3 text-center">
<label>Boxes to create: </label>
<input type="number" id="num_boxes_0">
</div>
<div class="col-3 text-center">
<label>Material: </label>
<br>
<select name="item_id" id="material_0">
{% for material in materials %}
<option value="{{ forloop.counter }}">{{ material }}</option>
{% endfor %}
</select>
</div>
<div class="col-3 text-center">
<button type="button" id="create_boxes_0" class="btn btn-danger">Create</button>
</div>

<!-- Nested forms with desired number of boxes -->
<div id="nested_forms_0">
<div class="row" id="box_0">
{% for child in child_form%}
<div class="col-3 text-center">
<h5>Child #1: </h4>
</div>
<div class="col-2 text-center">
{{ child.kg }}
</div>
<div class="col-2 text-center" >
{{ child.material }}
</div>
<div class="col-2 text-center" >
{{ child.box_price }}
</div>
<div class="col-3 text-center">
<button type="button" id='remove_box_0' class="btn btn-danger">Remove box</button>
</div>
{% endfor %}
</div>
</div>
</div>
<p>
{{ child_form.management_form }}
<input id="create" class="btn btn-secondary btn-lg btn-block btn-option btn-form" type="submit" value="Crear" />
</p>

javascript

// Adds the difference between desired boxes and current boxes
function add_boxes(form_id, total, num){
for (let i = total; i != total+num; i++) {
var element = $('#nested_forms_' + form_id + "> :first-child ").clone().css('display', 'none');
element.appendTo('#nested_forms_' + form_id).show('250');
};
$('#id_child-TOTAL_FORMS').val(num + total);
};

// Removes every unneeded boxes
function remove_boxes(form_id, total){
$('#nested_forms_' + form_id).children().each(function(){
if ($(this).index() >= total) {
$(this).fadeTo(150, 0).slideUp(150, function(){
$(this).remove();
});
}
});
$('#id_child-TOTAL_FORMS').val(total);
}

/r/django
https://redd.it/8ggdy2
Announcing Hack for the Sea 2018: Sept 22-23 in Gloucester MA

Hi everybody, we are happy and proud to announce the **3rd Annual Hack for the Sea** being held Sept 22-23 at the American Legion in Gloucester, MA

If you don't know, a "hackathon" is an attempt to ideate and make progress toward solutions to shared challenges. Hack for the Sea is slightly different than many other hackathons in that:

* It focuses on marine science exclusively
* All submissions must be released as open source
* It is meant to be a healthy, inclusive, and outcome-driven event.

This year's challenges come from our beneficiary organizations: the Consortium of Universities for the Advancement of Hydrologic Science, Inc. (CUAHSI), the Gloucester Marine Genomics Institute (GMGI), The Massachusetts Division of Fisheries and Wildlife, Ocean Alliance, and the MassBays National Estuary Program.

1. How does a changing coastal watershed impact coastal waters?
2. Where and when will Cod spawning occur?
3. Can a whale be identified based on their blowholes?
4. Can a mooring be designed that is not only eelgrass-friendly, but also user-friendly?

Ask me anything!

Early-bird Tickets are available here: https://www.eventbrite.com/e/hack-for-the-sea-2018-tickets-45603855359

/r/pystats
https://redd.it/8gqxoo
Update photo category with ajax.

So I want to implement a select drop-down element which on change should update the images shown based on the selected category. I created the models and referenced the "categories" model from a "post image" model. I can get these categories show up on front end in a drop-down. My question is how to implement this with ajax? I am not sure how url matching plays a role on this. TY

/r/djangolearning
https://redd.it/8goftk
pyc files cached? Problem in production.

hi i have site deployed using nginx and gunicorn.

I decided I wanted to change something in a views.py file in production.
None of the changes have taken place.
I have already deleted the .pyc files. I have restarted nginx and gunicorn
i have also used --reload.

is there another place in the project that these pyc files are being cached?

/r/django
https://redd.it/8gv51q
Can you test login in shell?

As the question says. My first Django project had so many errors related to the login/authentication by the user that I had to forget about the whole project without being able to fix that which was the most important thing.

Right now, I found an awesome way to test my database/models by using the Shell before even setting up views/urls/templates and I'm so happy by doing it this way(so much relief), but when it comes to authenticating and login a user... I dont know about that. It can be a thing? Testing login/authentication via Shell?

Anyone faced a similar situation? Any help would be appretiate it, thanks for your time also.

/r/djangolearning
https://redd.it/8gwlii
flask-praetorian v0.4 release

I'm proud to announce the release of flask\-praetorian 0.4 today. This version comes with some bug fixes and api hardening. Most significantly, though, some improved documentation and lots of tutorial.

Pull\-requests and issues are very welcome!

[http://flask\-praetorian.readthedocs.io/en/latest/](http://flask-praetorian.readthedocs.io/en/latest/)

/r/flask
https://redd.it/8gs9mf