Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Data Graham
Dec 28, 2009

📈📊🍪😋



Thermopyle posted:

I don't have time to type a lot right now, but one aspect of the purpose of putting it on the model or other utility modules is that you can't keep your code more generic by having it in your view.

I mean, if, as part of your view, you have code that does x, y, and z, how are you going to use that code elsewhere? What if you want a view later that wants x, y, and z along with other stuff?

Or (for a very common pattern), what if you want a view that feeds a template for a web app, and then also a Django REST Framework view creating an API for a mobile app? Both need to have the same data properties/methods, but only if you put that code in the model will you be able to avoid writing that logic twice.

E: For that matter DRF won't even help you unless your logic is in the model anyway.

Adbot
ADBOT LOVES YOU

NtotheTC
Dec 31, 2007


I get to start a brand new project at work soon, casting off the shackles of Django1.6 (because holy poo poo) and DRF < 3. The issue is I've now paralysed myself with indecision wondering how best-practice-y to get with it. Do I smash out a cookiecutter-django project template? Use LESS/SASS? Angular 1 or 2? Gulp vs Webpack? I have a fairly solid server-side stack that I'm comfortable with these days, but front-end wise I've been so far off the cutting edge for so long I have no idea where to start. Perhaps this is a question more suited for the modern web-dev thread but I'd like other django users' perspectives on how useful CSS preprocessors/JS toolkits are... Have we reached the point where I don't need to use NPM or Bower anymore?

a witch
Jan 12, 2017

Most people seem to seem to use Django to serve an API that gets consumed by a separate front end project. The front end is built with webpack, react/angular, etc. This is totally valid, and someone with more experience doing it will be along to explain it in depth.

If you don't want to go that route, it's still very easy to add modern front end tooling to a django project. I work with a large django project that takes this approach by using webpack to build our JavaScript and CSS. The output directory from the webpack build gets added to STATICFILES_DIRS, so we get all the advantages of modern front end tooling, while keeping all our existing django templates and staticfiles code.

This certainly isn't the approach that's en vogue, but it works very well.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

NtotheTC posted:

Have we reached the point where I don't need to use NPM or Bower anymore?

This questions seems to presuppose that that is a good and useful direction to go.

Personally, I try to use Django to do as little frontend as is possible. In my ideal project, Django serves nothing but an API via DRF, and then I use modern frontend tools like NPM (well, yarn actually), webpack, react, etc.

huhu
Feb 24, 2006

Thermopyle posted:

Anyone bought Two Scoops for 1.11 yet?

Enough new info for the price of upgrading from the 1.8 version?

Currently 4 chapters in and I'm loving this book, thanks.

epswing
Nov 4, 2003

Soiled Meat

Thermopyle posted:

Anyone bought Two Scoops for 1.11 yet?

Enough new info for the price of upgrading from the 1.8 version?

Bought it, haven't started reading yet, but I'll give a review when I get a few chapters in.

epswing
Nov 4, 2003

Soiled Meat
I have a conditional query question. I've been reading https://docs.djangoproject.com/en/1.11/topics/db/aggregation/ and https://docs.djangoproject.com/en/1.11/ref/models/conditional-expressions/ and can't seem to figure this out.

Let's say I have an Order model, and I want a payment breakdown grouped by user.

Here's the Order model:
Python code:
class Order(models.Model):    
    CASH = 'c'
    CARD = 'a'
    PAYMENT_TYPES = (
        (CASH, 'Cash'),
        (CARD, 'Card'),
    )
    user = models.ForeignKey(User, on_delete=models.PROTECT, null=True, blank=True)
    payment_type = models.CharField(max_length=1, choices=PAYMENT_TYPES)
    grand_total = models.DecimalField(max_digits=8, decimal_places=2)
Here's a values() + annotate() query showing me the total per user:
Python code:
query = Order.objects.values(
    'user'
).annotate(
    total=Sum('grand_total'),
)
The result, so far so good:
pre:
User     Total
--------------
User 1   300
User 2   250
However, when I add Case/When conditions to the query:
Python code:
query = Order.objects.values(
    'user'
).annotate(
    cash=Case(When(payment_type=Order.CASH, then=Sum('grand_total')), default=Value(0)),
    card=Case(When(payment_type=Order.CARD, then=Sum('grand_total')), default=Value(0)),
    total=Sum('grand_total'),
)
I get this result, which is not what I want:
pre:
User     Cash      Card      Total
----------------------------------
User 1   300       0         300
User 2   200       0         200
User 2   0         50        50
This is what I want:
pre:
User     Cash      Card      Total
----------------------------------
User 1   300       0         300
User 2   200       50        250
Why is the Case/When undoing the GROUP BY that values() is giving me?

Edit1: The Order model doesn't have default ordering, but just in case, upon reading interaction-with-default-ordering-or-order-by when using values(), I've tried adding .order_by() and .order_by('user') to my query, which did not change the results.

Edit2: Here's the SO post, if you care: https://stackoverflow.com/questions/45802068/why-does-case-when-ungroup-the-results-of-my-values-annotate-query

epswing fucked around with this message at 17:44 on Aug 21, 2017

Data Graham
Dec 28, 2009

📈📊🍪😋



Is there a way to force a reversed URL to contain a slash after an optional kwarg in the pattern?

I'm doing this:

code:
    url(r'^account/work/(?P<page>[a-z]+)?/?$', login_required(accounts_views.WorkView.as_view()), name='account-work'),
So I want these reverses to happen:

code:
account-work                /account/work/
account-work page=first     /account/work/first/
account-work page=second    /account/work/second/
But with the trailing slash in the pattern like that, as an optional, it never appends it in the reverse; I get like '/account/work/first'.

If I make the slash non-optional, then 'account-work' reverses to '/account/work//', and I don't want that.

Is there a better way to do this?

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
I would argue you shouldn't make them optional if you can. SEO practice is to either always have them or not, and permanent redirect to enforce the scheme, cause technically /thing/ and /thing are two separate URLs. Django even comes with an APPEND_SLASH option, but unfortunately not the reverse which I kinda like.

Data Graham
Dec 28, 2009

📈📊🍪😋



Yeah, that's my problem. I want it to always have the slash, but I can't seem to do that without the bare "work" URL getting an extra slash, /account/work//.

MonkeyMaker
May 22, 2006

What's your poison, sir?
I haven't tested this, but wouldn't

code:
url(r'^account/work/(?P<page>[a-z]+)/?$'
work?

That should still capture the page kwarg cleanly but require the trailing slash to be there.

Data Graham
Dec 28, 2009

📈📊🍪😋



No dice; says 'account-work' with no arguments doesn't match any patterns. 'account-work' with an arg specified works though.

Pumpkin Pirate
Feb 2, 2005
???????
How about this?
code:
^account/work/(?:(?P<page>[a-z]+)/)?$
That should make the page and / optional, but in a neither or both way.

MonkeyMaker
May 22, 2006

What's your poison, sir?
You can have more than one route that points to the same view.

code:
url(r'^account/work/$', login_required(accounts_views.WorkView.as_view()), name='account-work'),
url(r'^account/work/(?P<page>[a-z]+)/?$', login_required(accounts_views.WorkView.as_view()), name='account-work-page'),
Not super-elegant, I guess. Pumpkin Pirate's solution might work, too. I'm not even sure you need the "?:" non-capture part. Django would just pass the groups as args which you're likely ignoring anyway.

Data Graham
Dec 28, 2009

📈📊🍪😋



MonkeyMaker posted:

You can have more than one route that points to the same view.

code:
url(r'^account/work/$', login_required(accounts_views.WorkView.as_view()), name='account-work'),
url(r'^account/work/(?P<page>[a-z]+)/?$', login_required(accounts_views.WorkView.as_view()), name='account-work-page'),
Not super-elegant, I guess. Pumpkin Pirate's solution might work, too. I'm not even sure you need the "?:" non-capture part. Django would just pass the groups as args which you're likely ignoring anyway.

^^ This is byte-for-byte what I ended up doing, but it turns out Pumpkin Pirate's does work too. Nice! It does need the ?: though.

Thanks guys! :cheers:

epswing
Nov 4, 2003

Soiled Meat

Anyone think this a bug I should be reporting?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

epalm posted:

Anyone think this a bug I should be reporting?

I've never used case/when so I'm not sure if its a bug or not, but if I were you I'd report it as a bug only after trying their IRC. When you do report it, you'll either get someone telling you its not a bug and you need to do X, Y, and Z, or it'll be acknowledged as a bug.

epswing
Nov 4, 2003

Soiled Meat
I was going to get PyCharm with that 30% discount where proceeds go to the Django project, until I remembered that PyCharm is now a "subscription service". I seem to remember the Internet crying about this a year or two ago, and I thought JetBrains went back on it and said "ok ok we'll still offer standalone licenses" but I'm not seeing that option on their site.

Is everyone still outraged about this, or is this just how things are nowadays.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

epalm posted:

I was going to get PyCharm with that 30% discount where proceeds go to the Django project, until I remembered that PyCharm is now a "subscription service". I seem to remember the Internet crying about this a year or two ago, and I thought JetBrains went back on it and said "ok ok we'll still offer standalone licenses" but I'm not seeing that option on their site.

Is everyone still outraged about this, or is this just how things are nowadays.

You only have to keep paying the subscription fee if you want new versions. Otherwise you can continue to use whatever old version you have. Read more about it here: https://sales.jetbrains.com/hc/en-gb/articles/207240845-What-is-perpetual-fallback-license-

It seems like a perfectly reasonable subscription model to me.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Yeah, it's fine.

What is it...something like $60/year? I can't even remember now its so insignificant in the scheme of things.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Video talks from DjangoCon 2017 are up.

I waffled back and forth about going this year, but I eventually decided not to.

After skimming these videos I have to be honest and say I'm glad I didn't go. It seems way too beginner-focused this year which kind of boggles my mind. I would assume there's not a lot of beginners spending $700 or companies sending their junior developers to this thing...

Mulozon Empuri
Jan 23, 2006

Djangocon EU was also an incredible waste of time.

NtotheTC
Dec 31, 2007


Am I being too purist in wanting to separate the Django API backend entirely from the Angluar/whatever/JS frontend? Are there upsides or downsides to having the frontend A) In a completely separate repository (I feel like managing versioning cross-repo could be a hassle) or B) just in a standalone sub-folder within the django project. I don;t particularly want it in the django static files directory for django to serve as there should be no real need. At least not the source js files, does it make sense to have the javascript pre-processor task dump the files into the static folder for live?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

No, it's a good idea to have your front end and your backend as completely separate projects.

NtotheTC
Dec 31, 2007


Thermopyle posted:

No, it's a good idea to have your front end and your backend as completely separate projects.

Does it not get irritating having to update two different repos for tiny changes? Or is there a better system than the one i'm imagining where the django project builds the front end project (version x) as part of its deployment.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

NtotheTC posted:

Does it not get irritating having to update two different repos for tiny changes? Or is there a better system than the one i'm imagining where the django project builds the front end project (version x) as part of its deployment.

I mean, I guess its slightly less convenient to have to type 'git push' twice, but thats massively outweighed by the clarity of separation of concerns. It really helps keep your API design clean.

Besides, I find that once I'm past the brainstorming phase I'll very rarely be jumping back and forth between the two repos. A project I'm working on now is several months in to development and I haven't touched the backend repo in a couple weeks.

My deployment for the backend and the frontend is the same system...I push each to github, my CI system picks up that push, runs tests and then deploys to Amazon.

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!

NtotheTC posted:

Does it not get irritating having to update two different repos for tiny changes? Or is there a better system than the one i'm imagining where the django project builds the front end project (version x) as part of its deployment.

I think having a consistent approach to manage cross dependencies (env vars, shared static vars etc) and separate repos is the way to go.

huhu
Feb 24, 2006
I'm two weeks into my new job running the front and back ends completely separately and I must say it's very nice.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

huhu posted:

I'm two weeks into my new job running the front and back ends completely separately and I must say it's very nice.

I'm working on a project now where, out of laziness, I went against my normal workflow and I just set up my frontend inside one of my django apps.

High on my priority list is undoing that mistake. It didn't really save me much effort to begin with, and now its costing me.

huhu
Feb 24, 2006
code:
def request_access_token(code):
    ACCESS_TOKEN_URL = 'https://api.instagram.com/oauth/access_token'
    post_data = {
        'client_id': INSTAGRAM_KEY,
        'client_secret': INSTAGRAM_SECRET,
        'grant_type': 'authorization_code',
        'redirect_uri': REDIRECT_URI,
        'code': code
    }
    r_serialized = requests.post(ACCESS_TOKEN_URL, data=post_data)
    r_dict = json.loads(r_serialized.text)
    return r_dict
Is there anything wrong with getting an access token this way? Is there a better way?

Data Graham
Dec 28, 2009

📈📊🍪😋



Has anybody worked with https://pypi.python.org/pypi/django-pdfkit ?

Specifically, I'm having trouble with the rendered page apparently having a race condition where it may or may not be able to pull in all the dependency / include files at render time, like CSS and image files. If it doesn't pull them in in time, the page will render wrong or throw an error, depending. But sometimes it's perfectly fine.

I've found that the only way to avoid this is to include all the CSS inline, instead of as a separately linked file, even though the latter is supposed to work fine.

Anyone have any experience with this?


E: actually I think never mind, this seems to be a problem with wkhtmltopdf, not with Django; I tried another wrapper and it has the same behavior. Fooey.


E2: lol i'm just gonna do this

code:
class MyPDFView(PDFView):

    def get(self, *args, **kwargs):
        try:
            return super(MyPDFView, self).get(*args, **kwargs)
        except:
            logger.warning('PDF generation failed; retrying')
            return self.get(*args, **kwargs)
lmfao owned

Data Graham fucked around with this message at 21:17 on Oct 2, 2017

NtotheTC
Dec 31, 2007


Data Graham posted:


E2: lol i'm just gonna do this

code:
class MyPDFView(PDFView):

    def get(self, *args, **kwargs):
        try:
            return super(MyPDFView, self).get(*args, **kwargs)
        except:
            logger.warning('PDF generation failed; retrying')
            return self.get(*args, **kwargs)
lmfao owned

The day you forget to commit a static file for those pdfs...

"What do you mean the workers are all dead? I'll check the log files oh god"

Hed
Mar 31, 2004

Fun Shoe
Maybe less a Django question but I'm usually interested in implementing it in a Django context.

Is there a list of strategies for keeping change history in fields? I've had a couple toy projects where it would be useful to have snapshots in time, or history when portions were edited. I've played around with having archive flags and creating new objects to link together, but they all have drawbacks.

If this is a more general SQL thing I'd be interested in reading up, but haven't hit upon the correct phrase / terms yet.

epswing
Nov 4, 2003

Soiled Meat
Let's say my Django app needs to generate an image, and save it to disk. Of course, I don't want to do this in a view, I want to push the details of the work (a few id numbers, some text) onto a queue, and process items in the queue sequentially.

Many resources seem to point to using redis + celery. Rather than installing and maintaining several new dependencies, why wouldn't I use django-background-tasks (http://django-background-tasks.readthedocs.io/en/latest/)? It seems to just use Django ORM, which it polls "every few seconds". I guess this is considered slow, but for a low-traffic site that doesn't need to blaze through the queue at maximum speed, is there anything else wrong with django-background-tasks?

Hed
Mar 31, 2004

Fun Shoe
It works super well, it just sucks at scaling.

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!

Hed posted:

Maybe less a Django question but I'm usually interested in implementing it in a Django context.

Is there a list of strategies for keeping change history in fields? I've had a couple toy projects where it would be useful to have snapshots in time, or history when portions were edited. I've played around with having archive flags and creating new objects to link together, but they all have drawbacks.

If this is a more general SQL thing I'd be interested in reading up, but haven't hit upon the correct phrase / terms yet.

Audit trails

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

epalm posted:

Let's say my Django app needs to generate an image, and save it to disk. Of course, I don't want to do this in a view, I want to push the details of the work (a few id numbers, some text) onto a queue, and process items in the queue sequentially.

Many resources seem to point to using redis + celery. Rather than installing and maintaining several new dependencies, why wouldn't I use django-background-tasks (http://django-background-tasks.readthedocs.io/en/latest/)? It seems to just use Django ORM, which it polls "every few seconds". I guess this is considered slow, but for a low-traffic site that doesn't need to blaze through the queue at maximum speed, is there anything else wrong with django-background-tasks?

It works, it just won't scale well.

FWIW, I think 90% of people who use celery would be better served by python-rq. It's easier to use and configure. It doesn't support all the advanced features of celery, but like I said the vast majority of people don't need or use those advanced features anyway.

Personally, I'd just go with python-rq because redis is easy to install and maintain and I have a strong bias against small projects (python-41 has 4k+ stars on github, django-background-tasks has 117). They're more likely to become un-maintained, less likely to have security issues found and patched, you're less likely to be able to get help, etc...

epswing
Nov 4, 2003

Soiled Meat

Thermopyle posted:

It works, it just won't scale well.

FWIW, I think 90% of people who use celery would be better served by python-rq. It's easier to use and configure. It doesn't support all the advanced features of celery, but like I said the vast majority of people don't need or use those advanced features anyway.

Personally, I'd just go with python-rq because redis is easy to install and maintain and I have a strong bias against small projects (python-41 has 4k+ stars on github, django-background-tasks has 117). They're more likely to become un-maintained, less likely to have security issues found and patched, you're less likely to be able to get help, etc...

I hear you re: github stars. Hard to top a thriving community. When looking for 3rd party apps, the first thing I look at tends to be "date of last commit" on GitHub/BitBucket/etc.

RQ looks interesting, however after reading their Docs section, there's one part I don't understand: how do you start the worker process in production? The Starting workers page shows an example running $ rq worker, but... obviously I'm not going to leave an ssh session running with a user logged in after running this command.

epswing fucked around with this message at 19:18 on Oct 4, 2017

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Thermopyle posted:

Personally, I'd just go with python-rq because redis is easy to install and maintain and I have a strong bias against small projects (python-41 has 4k+ stars on github, django-background-tasks has 117). They're more likely to become un-maintained, less likely to have security issues found and patched, you're less likely to be able to get help, etc...

python-rq looks pretty cool.

I agree with the github stars thing most of the time. Occasionally there is a small project that is very simple and does what I need, like this one I've been using for years that has 66 stars and hasn't been updated since 2015: https://github.com/defrex/django-after-response

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Yeah, sometimes you just don't have any choice.

I try to look the code over on small, not popular projects and see if I'd be comfortable supporting it myself

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply