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
BigRedDot
Mar 6, 2008

KingNastidon posted:

I'm working on a django app that uses pandas and embedded bokeh plots for portfolio analysis with time series data. I'm looking to move the app to a shared hosting service like webfaction or dreamhost to demo to my bosses and argue my time should be spent on this versus things that actually generate revenue for the company.

My background is in engineering/stats and I have zero experience with the basics of moving an app into production on a non-localhost. What's the process for running the bokeh-server (or any shell process) on a shared host? During local development I'm able to just run the server using command prompt and let it sit idle. Would webfaction's shell access(http://docs.webfaction.com/user-guide/access.html) allow for this? This is probably a super naive question so I'm having a hard time finding relevant examples on searching google. Or for those familiar with Bokeh, is the bokeh-server not meant to be used in this way and I should into something else?

Hi, I'm the Bokeh project lead. The bokeh-server is definitely intended for people to be able to run, either on their own local on-premise assets, or in some hosted/cloud situation. There's several ways to run it: It has a Flask Blueprint, so if you are using Flask you can incorporate it directly into an existing Flask server. Otherwise, it can be run as a command-line tool (config options supplied on the command line as well) or, you can import the server and run it from python code. I don't know about the particulars of your deployment, but you would probably just want to start it using whatever startup script controls your deployment, then your django app can interact with it either by templating documents that load plots, etc. from the bokeh-server, or but storing plots and data on the server using the plotting APIs.

Adbot
ADBOT LOVES YOU

Hed
Mar 31, 2004

Fun Shoe
I may be misunderstanding your question but is this what you want?

code:

Airfield.objects.exclude(surface='').filter(runway__length__gte=8000)

Dominoes
Sep 20, 2007

I want to be able to allow any surface, if paved_only is False.

MonkeyMaker
May 22, 2006

What's your poison, sir?
So...

code:
Airfield.objects.filter(runway__length__gte=8000)
?

Dominoes
Sep 20, 2007

surface might have a value. Repeating the filter statement for each if/or doesn't make sense if there are multiple filter fields; ie the solution I posted, while convoluted, is cleaner. I'm looking for something like query_parameter=anything

king salmon
Oct 30, 2011

by Cowcaster
Are there any tools people can recommend for importing data when you don't have control of the source? I have a bunch of excel documents (and models I've written for them)- I can read the data from the documents just fine but it seems like writing the import code will be a huge unmaintainable mess since I'm basically duplicating my models code but in a different place.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

king salmon posted:

Are there any tools people can recommend for importing data when you don't have control of the source? I have a bunch of excel documents (and models I've written for them)- I can read the data from the documents just fine but it seems like writing the import code will be a huge unmaintainable mess since I'm basically duplicating my models code but in a different place.

I made a couple custom management commands for importing data into my app. No need to duplicate your models in that case, since you can access everything in your project from the management command.

Literally Elvis
Oct 21, 2013

As a first project, I'm turning a small personal script into something with a web interface using Django, and I can't for the life of me get configparser to work, even with just running locally.



I get a NameError (which basically means configparser is trying and failing to assign value from the .ini file) every time. Doesn't matter if I make it just creds.ini or /backend/creds.ini or /screlp/backend/creds.ini.

I've got the four values hardcoded for now, but it's obviously not wise to git those.

Also hello there little docstring incongruity, I see you!

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

Literally Elvis posted:

As a first project, I'm turning a small personal script into something with a web interface using Django, and I can't for the life of me get configparser to work, even with just running locally.



I get a NameError (which basically means configparser is trying and failing to assign value from the .ini file) every time. Doesn't matter if I make it just creds.ini or /backend/creds.ini or /screlp/backend/creds.ini.

I've got the four values hardcoded for now, but it's obviously not wise to git those.

Also hello there little docstring incongruity, I see you!

You should generally define a DJANGO_ROOT variable in your settings that's the root path of the whole project. Your config.read() should call os.path.join(DJANGO_ROOT, etc., etc., etc., etc.) to point to the creds file.

Alternatively (and probably as a better practice), credentials should be defined as environment variables and read in using os.environ. Generally keys, credentials, and sensitive information like passwords should be defined as environment variables so as to keep them out of source.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
With the OP being 6 years old at this point, is there a recommend book for learning the basics of Django? A Django for non-programmers book? I've been teaching myself Python to help out at work and I've interested in learning and creating a web app.

Hughmoris fucked around with this message at 01:36 on Oct 5, 2014

Thermopyle
Jul 1, 2003

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

Hughmoris posted:

With the OP being 6 years old at this point, is there a recommend book for learning the basics of Django? A Django for non-programmers book? I've been teaching myself Python to help out at work and I've interested in learning and creating a web app.

I think most of the django books are kinda old at this point. Django updates semi-frequently.

Have you done the official tutorial? If not, maybe try that and ask questions about it here.

The book that has been talked about the most in this thread recently is Two Scoops of Django, but that's less for teaching beginners, and more for laying out best practices for people already using Django. Now, that's not to say you shouldn't read it. You should definitely read it.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Thermopyle posted:

I think most of the django books are kinda old at this point. Django updates semi-frequently.

Have you done the official tutorial? If not, maybe try that and ask questions about it here.

The book that has been talked about the most in this thread recently is Two Scoops of Django, but that's less for teaching beginners, and more for laying out best practices for people already using Django. Now, that's not to say you shouldn't read it. You should definitely read it.

Thanks. I tried walking through the official tutorial but I was quickly overwhelmed. Its clear I need to put more time in learning objects and classes.

MonkeyMaker
May 22, 2006

What's your poison, sir?
FWIW, I'll be doing a Django beginner series at Treehouse in a few months, probably early 2015.

ozmunkeh
Feb 28, 2008

hey guys what is happening in this thread
Using database-backed sessions, is PickleSerializer always a bad idea? The docs say to not use it with cookie-based session data but I've seen calls elsewhere to just not ever use it at all.

I'm updating an old app originally written in 1.3 which stores a lot of datatypes the JSONSerializer doesn't seem to like (decimals, dates). I probably should rewrite the whole thing anyway but I was hoping to avoid that right now.

NtotheTC
Dec 31, 2007


Does anyone serve angularjs (or any other kind of 1-page html/js) apps through django or have good ideas how to do it? I'm trying to think of a neat way to achieve it and keep running into knotty problems. It seems like you want to have a django app for your angularjs app, that has AppView/API URLs, and a templates folder with an index.html that somehow includes your angularjs app .html, but getting this to work with my versioned, cache-buster js/css produced by grunt tasks etc seems like a challenge.

Yay
Aug 4, 2007

ozmunkeh posted:

I'm updating an old app originally written in 1.3 which stores a lot of datatypes the JSONSerializer doesn't seem to like (decimals, dates).
The design decision to not use the DjangoJSONEncoder (used for model serializing to JSON) for sessions was pretty stupid IMHO, but IIRC its fairly trivial to subclass the JSONSerializer to use it.

And yes, if you value your sanity, just don't use the pickle one at all.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

Yay posted:

And yes, if you value your sanity, just don't use the pickle one at all.

That's a bit ominous. Care to share why not?

Gounads
Mar 13, 2013

Where am I?
How did I get here?

NtotheTC posted:

Does anyone serve angularjs (or any other kind of 1-page html/js) apps through django or have good ideas how to do it? I'm trying to think of a neat way to achieve it and keep running into knotty problems. It seems like you want to have a django app for your angularjs app, that has AppView/API URLs, and a templates folder with an index.html that somehow includes your angularjs app .html, but getting this to work with my versioned, cache-buster js/css produced by grunt tasks etc seems like a challenge.

I put my angular stuff in staticfiles, and then end up serving them off of a CDN. The only angular in a django template is the reference to the script and a root node tagged with ng-app (and usually a directive that loads a template).

I usually also make an API app that the angular client talks to.

NtotheTC
Dec 31, 2007


Yeah, that's all logical stuff :) thanks. I think i really need to move away from serving apps via django to be honest. Its just not very robust.

As an aside: How do people here like to setup their dev environment at home? I'm disliking my current situation which is a dual boot of Ubuntu on my windows machine, its a hassle to power down/boot up a different os if I want to do something else. I've heard people talk of using virtual machines, which sounds fine but is there any way to run them so that they "feel" native while you're using them? As in span across multiple monitors seamlessly and just have a key combo or something to switch back to windows.

Failing that I guess I buy a dev laptop and just plug it in when needed, portable solution but expensive

Mulozon Empuri
Jan 23, 2006

How about using Vagrant? You develop as normal on your windows box, but the code runs in the vm.

NtotheTC
Dec 31, 2007


Id want to do my development in a Linux environment, I just couldn't get used to using windows to develop again. The issue is I do like vagrant and the ability to build/teardown a server that runs the code that has the exact same config as the production box. But then I'm running vagrant in a Linux vm and things get complicated possibly.

Thermopyle
Jul 1, 2003

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

NtotheTC posted:

Yeah, that's all logical stuff :) thanks. I think i really need to move away from serving apps via django to be honest. Its just not very robust.

As an aside: How do people here like to setup their dev environment at home? I'm disliking my current situation which is a dual boot of Ubuntu on my windows machine, its a hassle to power down/boot up a different os if I want to do something else. I've heard people talk of using virtual machines, which sounds fine but is there any way to run them so that they "feel" native while you're using them? As in span across multiple monitors seamlessly and just have a key combo or something to switch back to windows.

Failing that I guess I buy a dev laptop and just plug it in when needed, portable solution but expensive

I run Windows and dev in a virtual machine running Ubuntu. The virtual machine occupies two of my three monitors.

If I covered the third monitor, you'd never know it was a VM.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

NtotheTC posted:

Id want to do my development in a Linux environment, I just couldn't get used to using windows to develop again. The issue is I do like vagrant and the ability to build/teardown a server that runs the code that has the exact same config as the production box. But then I'm running vagrant in a Linux vm and things get complicated possibly.

Instead of spinning up local VMs with Vagrant you could spin up EC2 VMs with Vagrant

NtotheTC
Dec 31, 2007


Thermopyle posted:

I run Windows and dev in a virtual machine running Ubuntu. The virtual machine occupies two of my three monitors.

If I covered the third monitor, you'd never know it was a VM.

Do you use Vagrant or similar to handle the dev environment or is it run locally? I'm not completely against the idea of just using django's runserver to run the code locally on a dev VM, but I do like "safety net" of running my code in the same environment as my production.

fletcher posted:

Instead of spinning up local VMs with Vagrant you could spin up EC2 VMs with Vagrant

This is very interesting, I wasn't aware you could do this. I'm having trouble deciphering AWS's pricing structure though- it seems to be a case of "Don't worry we'll just take the money if you do anything expensive" but I'd like to know roughly how much I'd be spending or whether I could just mooch around in the free-tier. I guess I'd be averaging no more than 80 hours of dev time a month + some cloud storage of VM images/backups etc.

Thermopyle
Jul 1, 2003

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

NtotheTC posted:

Do you use Vagrant or similar to handle the dev environment or is it run locally? I'm not completely against the idea of just using django's runserver to run the code locally on a dev VM, but I do like "safety net" of running my code in the same environment as my production.

Nah, it's just run locally.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

NtotheTC posted:

Do you use Vagrant or similar to handle the dev environment or is it run locally? I'm not completely against the idea of just using django's runserver to run the code locally on a dev VM, but I do like "safety net" of running my code in the same environment as my production.


This is very interesting, I wasn't aware you could do this. I'm having trouble deciphering AWS's pricing structure though- it seems to be a case of "Don't worry we'll just take the money if you do anything expensive" but I'd like to know roughly how much I'd be spending or whether I could just mooch around in the free-tier. I guess I'd be averaging no more than 80 hours of dev time a month + some cloud storage of VM images/backups etc.

I use both Django's runserver during development but then I use a Vagrant VM (provisioned with Chef) for testing before I push it to production.

Try the free tier first, if you outgrow it, it's really inexpensive to run an EC2 VM for only 80 hours a month. The storage and all that is super cheap, just calculate your monthly cost on the instance price per hour * number of hours per month.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I want to store a latitude & longitude in a Django model and display the points on a Google Maps widget. My database is PostgreSQL. Should I just use floats? django-geoposition? GeoDjango?

Dominoes
Sep 20, 2007

fletcher posted:

I want to store a latitude & longitude in a Django model and display the points on a Google Maps widget. My database is PostgreSQL. Should I just use floats? django-geoposition? GeoDjango?
Floats; one for lat, one for lon. No minutes/seconds, just degrees with decimals. Let me know if you have specific questions on drawing on Gmaps; I built a somewhat specialized Django/JS module.

Dominoes fucked around with this message at 12:01 on Oct 29, 2014

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Right on, thanks Dominoes!

I'm starting a new project using Django Rest Framework (which I've used previously) and React (which I have read about but not used yet). Any tips I should keep in mind? I think Thermopyle was doing some DRF/React stuff. Should I just have 1 Django view (index) and make it a single page app?

Thermopyle
Jul 1, 2003

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

fletcher posted:

Right on, thanks Dominoes!

I'm starting a new project using Django Rest Framework (which I've used previously) and React (which I have read about but not used yet). Any tips I should keep in mind? I think Thermopyle was doing some DRF/React stuff. Should I just have 1 Django view (index) and make it a single page app?

I think DRF is the best at what it does, but it's still a lot of work when you go beyond the very basics.

POSTing nested models just isn't worth the effort. In many ways, you're better off doing a POST to create the related model, and then a POST to create the parent model.

If that's not clear I'm talking about like if you have two models, ModelOne and ModelTwo. ModelOne has a FK field to ModelTwo. It's convenient to have an API endpoint for ModelOne return the data for ModelTwo nested inside, like:

code:
{id: 7,
 model_two: {id: 13, whatever: "hi"},
 some_field: "your mama"
}
Getting that from the API is easy. Making it so you can POST that kind of data is irritating, so I just do a POST to create an instance of ModelTwo, then a POST to create a ModelOne instance.

The POST to create a ModelOne instance after I've created ModelTwo would look like this:

code:
{
 model_two: "http://blahblahblah/api/modeltwo/13/",
 some_field: "your_mama"
}
That's easy with DRF. I'd rather it be easy to do the nested POST, but whatever.

As far as your views...you can do it any way you want. I've been transitioning a complex project over to React, and I've got multiple views. If you want to do a SPA, it's easy enough, just be aware that you'll be writing a lot of JS!

NtotheTC
Dec 31, 2007


Thermopyle posted:

POSTing nested models just isn't worth the effort. In many ways, you're better off doing a POST to create the related model, and then a POST to create the parent model.

I struggled with that recently. Good news on the DRF3 front though...

https://www.kickstarter.com/projects/tomchristie/django-rest-framework-3

quote:

The most pressing need for Django REST framework is a major overhaul of the serializers. This is currently the most awkward and complicated part of the project and it is in need of serious improvement. The aims of redesigning the serializers API would be:

  • A much cleaner, simpler API & implementation.
  • Support and documentation for writing completely custom serializer implementations, to provide for custom validation or output styles and non-ORM data sources.
  • Better support for validating and restoring complex nested structures.
  • Better support for using and styling serializers as regular HTML forms
  • Better documentation around using views as both Web pages and API endpoints.

Thermopyle
Jul 1, 2003

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

Even though your API consumer has to do more round trips, the clean separation of concerns on the backend is actually pretty nice. It makes the views and serializers simpler.

If DRF3 can keep that simplicity while better handling nesting, I'll be ecstatic.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
With DRF + React, how do you get your api urls using the {% url %} tag into javascript land?

ephphatha
Dec 18, 2009




What's everyone using for rich text input these days? I was using django-markupfield but it's not being updated for 1.7 due to architecture issues or something. Given that I'm only making a simple personal website and I've got no content up there already I'm not attached to it enough to consider rolling back to older versions of Django. Plus it'd be nice to use a fancier input widget that would show a preview mode since I tend to gently caress up the markdown syntax often.

I'm looking at django-epiced since it appears to prerender the markdown text and saves the html alongside the original in the database which is something most other fields/widgets don't seem to do (not that load is going to be at all a concern, I just don't think rendering the markdown every page load is ideal considering the source text is rarely going to change), plus it actually defines a field so I don't need to do anything special to use it. I'd love other suggestions though.

Storgar
Oct 31, 2011
My ModelViewSets disappeared from the root api JSON of my Default Router. I don't know the words to google for help. What do I do guys? :(

Thermopyle
Jul 1, 2003

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

Storgar posted:

My ModelViewSets disappeared from the root api JSON of my Default Router. I don't know the words to google for help. What do I do guys? :(

So they were showing and then they disappeared?

What did you change?



For my own question....has anyone dealt with handling a writable GenericForeignKey in django-rest-framework? I can get a readable hyperlinked representation like so:

Python code:
class SomeSerializer(serializers.HyperlinkedModelSerializer):
	content_object = serializers.SerializerMethodField('get_content_obj_url')

	def get_content_obj_url(self, obj):
		obj = obj.content_object

		view_name = get_view_name(obj)
		s = serializers.HyperlinkedIdentityField(source=obj, view_name=view_name)
		s.initialize(self, None)
		return s.field_to_native(obj, None)
(get_view_name(obj) is just a simple function for returning the DRF default view name of modelname-detail)

But, obviously, that doesn't work for POST'ing and today I can't think of a good way of supporting this.

Storgar
Oct 31, 2011

Thermopyle posted:

So they were showing and then they disappeared?

What did you change?

Strange... I tried messing with the url this time because before I was confident it wasn't the problem. I had this line:

code:
url(r'^api(/?)', include(router.urls)),
if I change the url regex to either "r^api", "r^api/", or "r^api/?", it shows hyperlinks to my viewsets again. Do you know what I'm doing with the parentheses?

Thermopyle
Jul 1, 2003

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

Storgar posted:

Do you know what I'm doing with the parentheses?

Yes.

Breaking your api root view.


Why do you think you need the parentheses there? I mean, I know what capturing groups do in an url regex, but why do you think you need them?

Storgar
Oct 31, 2011

Thermopyle posted:

Yes.

Breaking your api root view.


Why do you think you need the parentheses there? I mean, I know what capturing groups do in an url regex, but why do you think you need them?

I just wanted to group the '/' and the '?'. If I wanted to apply the ? operator to more than one character, I would need the parentheses right? I figured 'api(/?)' and 'api/?' should both match 'api' and 'api/'?

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

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

Storgar posted:

I just wanted to group the '/' and the '?'. If I wanted to apply the ? operator to more than one character, I would need the parentheses right? I figured 'api(/?)' and 'api/?' should both match 'api' and 'api/'?

When you capture a group in an url regex it gets passed to the view which is the second parameter of the url function. It looks like either django or DRF is choking on this unexpected group getting passed to it.

Whatever it is that is happening exactly it doesn't matter, because you don't need to do this at all. DRF will already handle urls with or without the trailing slash.

Just put url(r'^api/', include(router.urls)), and you'll get your api root whether you visit blahblah.com/api or blahblah.com/api/ if you're using the SimpleRouter or the DefaultRouter .

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