I am in the process of building a website with Django. The web app is for a local store in my city. The store sells gardening courses and workshops (the store is my mom's btw).
I want the admin of the page (the owner of the store) To be able to add new courses whenever she wants.
So I made this model for the courses:
class Curso(models.Model):
title = models.TextField()
photo = models.ImageField(upload_to='cursos/')
description = models.TextField()
price = models.IntegerField(null=False)
content = models.JSONField(blank=True)
clases = models.IntegerField(default=1)
duration = models.IntegerField()
isworkshop = models.BooleanField(default=False)
I included an ImageField because in the fronted there should be an image related to the course.
I added the path /cursos/ but I actually have no idea where the image is going. I saw people had a "media" folder.
But I also read the "media" folder was only for images uploaded by the user?
This is my static folder looks like:
static
|_app
|_js
|_css
|_sass
|_icons
|_images
|_homeslider
|_plants
|_pictures
Should the images uploaded from the admins app in that same folder?
Anyways I don't know where to store the images. And I don't have a hosting yet which leads me to my next question:
What should I do with the database?
I saw the majority of people asking these types of question had already bought a hosting so:
Should I buy a hosting even if the app is far from being ready just to start testing things out there and putting the courses objects there?
Are files stored differently in production?
Can I have my database locally and then upload it to the server?
What I don't want is to put a bunch of images and data on a sqlite database and then have to change all of it because that's not going to work for production
I'm new to web development and I am really lost when it comes to hosting, production and databases.
I would be very thankful for any help. I don't know what to do and I need someone more experienced to put me in the right direction.
Thank you in advanced!
Related
I do not really understand how the database works when using in production.
My stack:
Django
Heroku
AWS S3
PostgresSQL on Heroku
Users can generate some images on my app. The images are saved to AWS S3, and in some feature I want to retrieve the last generated image.
This below is my model where the images are saved in.
models.py:
class imgUploadModel(models.Model):
auto_increment_id = models.AutoField(primary_key=True, default=True)
image = models.ImageField(null=True, blank=True, upload_to="images/")
And here the view where the images is taken again and handled in some features.
view.py:
imgname = imgUploadModel.objects.all().last().image
As you can see I use .last() to get to the latest images which was generated.
Now to my questions:
In production, could it be that one user sees another users images? Or how does the Dynos (from heroku) separate the sessions?
Since the AWS S3 bucket is just a memory storage without dividing it by users, I assume that one user can see other users images. Especially then, when user A creates an Img, and user B clicks on 'latest image'.
If it is so, how can I create Dynos or Buckets or anything else to prevent this behaviour.
I just do not really understand it from a logical point of view.
I have a Django model which includes an Image Field, and an app which consumes the model instance including all images. The problem I'm having now is while my data and the img local link resides on my AWS RDS database, the media folder which contains the images for the model is not. Thus every time I deploy a new version of the app (I'm using Beanstalk for deployment), my media folder is wiped clean and all my image link dies as a result.
Below is my model:
class Item(models.Model):
# Fields
name = models.CharField(max_length=30, help_text="Enter item name")
description = models.TextField(help_text='Enter a short description')
image = models.ImageField(help_text='Upload item image', upload_to='image/item/',
default='image/demo.jpg')
# Methods
def __str__(self):
return str(self.name)
def get_absolute_url(self):
return reverse('model-detail-view', args=[str(self.id)])
And I use serve to retrieve the image from Django.
url(r'^resource/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
Now what would be the best way to resolve this problem? I would not want to upload the image to another server then link back to Django, it's best if I can save it to my database the moment I upload the file from my model form.
All feedback is appreciated.
See the EB documentation on design considerations:
Elastic Beanstalk applications run on Amazon EC2 instances that have no persistent local storage. When the Amazon EC2 instances terminate, the local file system is not saved, and new Amazon EC2 instances start with a default file system.
So, you cannot do what you are asking. You must upload the files to a persistent storage system; S3 is the obvious choice.
My company will be rolling out a new website to accompany our product launch and would like to switch over to Wordpress as our content management system. We will be utilizing a Wordpress theme that will allow users to create their own virtual events without having to log into the Wordpress dashboard (back-end). This event information will be displayed on the website for other users to view and register - this is all built into the theme we have purchased.
These virtual events will be held on our software platform, which is built on Django. We would like to utilize Wordpress to manage the login and event creation process, but would also like to have event information displayed on the Wordpress site AND imported to the Django database as well.
For example: Users will need to submit three items on the front-end Wordpress site to create an event: Title, Host Name, and Start Time. When that information is submitted can it be automatically duplicated to the Django database in addition to it being sent to the WP database?
I have already done some research on this matter, but what I have found thus far might not work for our needs. I found this presentation by Collin Anderson - it is similar to what we want to achieve, but I believe the application is a little different: http://www.confreaks.com/videos/4493-DjangoCon2014-integrating-django-and-wordpress-can-be-simple.
I have a lot of experience with Wordpress, but very limited experience with Django. This question is more for research purposes than a "how-to". We want to know if we can continue to plan on heading toward the Wordpress direction or if we should seek alternative methods for our site. I appreciate you taking moment to answer my question.
I'm working on something similar at the moment and found a good starting point was this:
http://agiliq.com/blog/2010/01/wordpress-and-django-best-buddies/
That way, as dan-klasson suggests, you can use the same database for both the wp side and the django side.
In short, first things first take a back up of the wp database in case anything goes wrong.
Create a new django project and set your settings.py to use the wp database.
In this new django project you can use ./manage.py inspectdb > models.py to autogenerate a models.py file of the wp database. Be careful here as there are differences between wp and django conventions. You will need to manually alter some of the auto generated models.py. Django supplies db_table and db_column arguments to allow you to rename tables and columns for the django part if you'd like to.
You can then create a new django app in your django project and place the models.py you've created in there. This new app will be using the same data as your wordpress site. I'm not sure exactly what you want to do but I would be very, very careful about having wordpress and django access the same data simultaneously. You may want to set the django side as read only.
You can then add other apps to extend the django side of things as you wish.
I should point out that I haven't completed my work on this yet but so far so good. I'll update as I find sticking points etc.
I am developing an app in Django 1.6 and would like to know if the photos that I upload via the admin interface are "static files" in Django terminology.
I have this model:
from django.db import models
class ShowroomDetail(models.Model):
title = models.CharField(max_length=1000)
description = models.CharField(max_length=4000)
class ShowroomPhoto(models.Model):
showroom = models.ForeignKey(ShowroomDetail, related_name='photos')
photo = models.ImageField(upload_to='images/')
which I using as a basis of developing a page where there can be one or more images displayed along with the title and description. The images will only ever be uploaded by the admin interface and more photos for the page may be added at a later date.
So are these uploaded photo's "static files"?
No. An image uploaded through the website will be stored in the MEDIA_ROOT folder. It is considered then as a media file and not a static file. For your example, if MEDIA_ROOT = '/path/to/project/media/, then your photos will be stored in media/images/.
The difference is staticfiles will be collected for each application and are part of the code. Typically it is image for the design, CSS files, Javascript, etc. They are necessary to run the project.
Media files are files uploaded through the administration or by user through a special application to add content (avatar, gallery image, message attachment...). This content is independent of the code of the project and is considered as content.
Related: What is the difference between static files and media files in Django?
"static files" as a term of art refers to the static files, and the location you configure for it. Uploaded files could be found by static files if you upload them to a location which static files uses. Obviously, security issues abound.
I'm writing a project in Django that has user uploaded images related to products, so my product model is something like:
class Product(models.Model):
name = models.CharField(max_length=128)
description = models.CharField(max_length=255)
owner = models.ForeighKey(User)
image = models.ImageField()
I would like to store the images at Amazon S3, since it's pretty cheap and fast. I would, as others, like to avoid the overhead of uploading the file to my server and then to S3. There are some sample code explaining how to upload a file directly to S3 from the client browser.
The issue I see (and I did not find any solution yet) is that I don't have only an image to upload. My object (Product) also has other fields, such as name, description and so on. All the examples I saw use one form for the image only. I would like to have one html form (with image, name and so on) for the user and once the user click on "submit", I would be able to store the data in amazon S3 and then the other info (name and so on), on my local database.
According to the image in http://docs.amazonwebservices.com/AmazonS3/latest/dev/UsingHTTPPOST.html using a POST method, it's possible to send data from the client to amazon S3 and my webserver. It's now clear if both situations can be included in the same page (same html form).
According to Amazon docs, It's possible to upload a file and set the redirect ULR when the upload is successful.
1) Is it possible to upload the image to the URL and then redirect to my webserver to store the rest of the information? Does the redirect keep the POST data (name, description etc)? How can I access the name of the file stored in S3?
2) Is there any other way to achieve my goal (besides my option #1)?
Thanks in advance
Checkout django-storages (http://code.welldev.org/django-storages/) it does exactly what you need.
And you don't have to care about multiple forms/redirects etc etc, it will replace the default file storage backend and just push files to amazon s3 bucket.
It supports several kind of storages and S3 in one among them, I've used for several projects so far and it's really easy to plug in.
Since docs seems pretty dead, here's my configurations:
settings.py
DEFAULT_FILE_STORAGE = "storages.backends.s3boto.S3BotoStorage"
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
AWS_STORAGE_BUCKET_NAME = ''
and of course you need to install django-storages