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
so. just spend 7 hours trying to set up my Android app to connect with django's csrf. let me save you some time

all of the everything about connecting to django using csrf and authentication talks about AJAX, and there is very little information about actually connecting with Android. Throw in the fact that Android httpurlconnection objects are obtuse, have terrible documentation, and there are no good tuturials about cookies that don't use some random persons half-baked personal library, and you get me spending 7 hours trying to get my app to log in.

so here heres how to do it.

#YOU NEED TO SET THE FULL COOKIE, NOT JUST THE CSRF TOKEN

**WRONG:**

csrftoken=3JR5KZ4tHrDRlPvVwufhrT8Zn83sklwk;

**CORRECT:**

csrftoken=3JR5KZ4tHrDRlPvVwufhrT8Zn83sklwk; expires=Tue, 13-Nov-2018 00:29:33 GMT; Max-Age=31449600; Path=/

grumble grumble grumble

I literally went into django.middleware.csrf and put in a bunch of print statements to try to figure out what was happening.

edit:

heres the full android code for setting the cookies (lots of non-important details omited):

URL url = new URL(serverURL + myurl);
//URL url = new URL(myurl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type","application/json");
//getFullCSRFcookie() returns the full cookie string with time stamps and everything
urlConnection.setRequestProperty("Cookie", getFullCSRFcookie());
urlConnection.connect();


Edit: love how the only comments basically boil down to "I don't know what your project is, but you're doing it wrong." Lol you keep being you, reddit

/r/django
https://redd.it/7crsn5
Hey, here's a very active discord server based around python and coding help, and many other tech topics. Come join our python and raspberry pi discord server! 2.6k members.
https://discord.gg/bB5vZSy

/r/Python
https://redd.it/7cqhu6
Flask Beginner

I've started to read Flask Web Development by Miguel Grinberg. I am on part two where we create a social blogging website. I feel somewhat uncomfortable with the information. It feels quite over whelming. Is this book a fast pace book or is flask from a beginner still quite complicated?

/r/flask
https://redd.it/7crxog
Maintain Production Server

I recently moved my Django app from Heroku to DigitalOcean to save some $$$. In my reading prior to making the switch, people noted that managing your own server on DO is more work than Heroku. They mentioned security patches and other maintenance that is required.

My question is, now that my app is up and running on DO, what ongoing maintenance should I be doing to keep it secure and running smoothly?

/r/django
https://redd.it/7cw24q
Which mobile app dev. framework do you recommend for use w/flask?

As far as user auth, i am using flask-login.

Any u guys recommend that's fast, easy, and cross platform? Was thinking react native..

/r/flask
https://redd.it/7cwzg6
How does Reddit implement its upvote/downvote system?

I'm building a nearly identical system, except there is no downvote, only upvote. You can only vote once for each post/comment.

How is their upvote system implemented? Do they use AJAX or some asynchronous system to make it so that making a vote does not refresh the page?

Sorry for the semi-broad question.

/r/django
https://redd.it/7czebj
What's everyone working on this week?

Tell /r/python what you're working on this week! You can be bragging, grousing, sharing your passion, or explaining your pain. Talk about your current project or your pet project; whatever you want to share.


/r/Python
https://redd.it/7cvtc5
Does IPyLua actually work?

It seems like [the IPyLua project](https://github.com/pakozm/IPyLua) on GitHub ought to work, but the instructions to make it actually work seem to be out of date. In particular, it says to

* Create a profile with IPython:

ipython profile create IPyLua

* Modify the profile's `ipython_config.py` to use IPyLua. This
will be at either `~/.config/ipython/profile_IPyLua/ipython_config.py` or
`~/.ipython/profile_IPyLua/ipython_config.py`:

# Configuration file for ipython.

c = get_config()

c.KernelManager.kernel_cmd = [
"luajit", # select your Lua interpreter here (lua5.2, lua5.1, luajit)
"IPyLua/IPyLuaKernel.lua", # probably need full path
"{connection_file}"
]

# Disable authentication.
c.Session.key = b''
c.Session.keyfile = b''

which doesn't seem to match up with current config file layout and options.

It would be cool to have working Lua, even if it's a bit simplistic and not offering much more than the command-line repl.

/r/IPython
https://redd.it/7d0v8o
for loop with range in template

In my template I want to iterate through multiple lists at the same time, so to me it makes sense to use a range. So for example, I want to do something like:

{{a.0.data}} {{b.0.data}} {{c.0.data}}
on the first loop, and on the second loop:

{{a.1.data}} {{b.1.data}} {{c.1.data}}
a,b,c are lists and the 0's are indexes that iterate.
In my views.py I added

{"range": range(10) to the context.}
Now if I do

{% for i in range %}
<p>{{i}}</p>
{% endfor %}
It works fine, but If I try something like:

{% for i in range %}
{{a.i.data}} {{b.i.data}} {{c.i.data}}
{% endfor %}
to try to iterate through the indexes It doesn't seem to work. Is there a proper way to go about this?
edit: formatting

/r/djangolearning
https://redd.it/7d1b04
Model inheritance: Can you access a child object from the parent object?

* I have 2 classes : Class A & Class B.
* Class B inherits from Class A. Example:: **Class B(A)**
* Class A is not Abstract
* When I create a Class B object, it also creates a Class A object

Can you access the Class B object from the corresponding Class A object? Example: **classa_object.classb_object** ?

I'm trying to reference the Class B objects from a Class A queryset.

EDIT:
Here is [the code](https://gist.github.com/emilepetrone/40469ea24f4b365473cf57e44320affa) I'm reffering to. I broke the class Transaction up into child models of the various status options.

"The problem I thought I had was a class starting to have too many fields only being used when the object was in certain states. 6 different states, different fields used in different states. I was thinking I should break that up into Parent (the fields they all use), and then Children models for the subset each uses.

Maybe I'm thinking about it wrong?

The example:

class Transaction

states: buy, sold, dividend, withdrawl etc"

/r/django
https://redd.it/7d0fn6
Return only the form in the UpdateView class ?

I am trying to switch form a functional view that edits and submits a form to class based view, UpdateView. I need to access only the form.

Until now I use this view:

@login_required
def edit_sale_view(request, id):
instance = get_object_or_404(Sale, id=id)
form = EditSaleForm(instance=instance)
if request.method == "POST":
form = EditSaleForm(request.POST, instance=instance)

if form.is_valid():
obj = form.save(commit=False)
obj.user = request.user
obj.save()
return JsonResponse({'msg': 'Data saved'})
else:
return JsonResponse({'msg': 'Data not saved'})
return HttpResponse(form.as_p())

The form.as_p() is important because it is used in the form that is displayed in a modal window.

So, I think that I need to find out how to return only the form and not the whole template.







/r/django
https://redd.it/7d3gwy