Lets say I have this model
class Egg(models.Model):
file = FileField(upload_to='media')
img = ImageField(upload_to='media')
How to get an Egg instance if I only have the file URL string like 'http://example.com/media/spam.tar.gz'? Can I query by this URL??
Which part of the url you want to query by? By spam.tar.gz? If so, you can try:
Egg.objects.filter(file__icontains='spam.tar.gz').
Related
I have a django project where url is like this
url(r'^invoice/(?P<invoice_id>[A-Za-z0-9]+)/(?P<order_id>[A-Za-z0-9]+)$',GenerateInvoicePdf,name='invoice'),
which generates url localhost:8000/invoice/2341wq23fewfe1231/3242
but i want url to be like localhost:8000/invoice?invoice_id=2341wq23fewfe1231&order_id=3242
i tried documentation and used syntax like this re_path(r'^comments/(?:page-(?P<page_number>\d+)/)?$', comments), But did not get desired result.
how can i do so?
The parts which you are trying to write after ? is called url query string. You don't need to define them in the urls.py. You can just use:
re_path(r'^comments/$', comments),
And inside comments views, you can access the query string like this:
def comments(request):
invoice_id = request.GET.get('invoice_id')
order_id = request.GET.get('order_id')
# rest of the code
I want to construct the following url
domain/edit/xray/<id>/<image_name>
where id is a model's id and image_name is the name of an image.
I have an app that will handle image editing with its own views lets say it mypil
mypil.views
def edit_xray(request, id, image_name):
....code to get image by name
....model by id and edit image using PIL
model = Model.objects.get(pk=id)
project/urls.py
url(r'^edit/', include(mypil.urls))
mypil.urls
url(r'^xray/(?P<id>)\d+/(?P<image_name>)\w+\.\w{3}$', mypil.views.edit_xray)
But when i try to view edit/xray/1/image.jpg (both id and image exist) i encounter this problem.
invalid literal for int() with base 10: ''
and the traceback shows me that line above
model=Model.objects.get(pk=id)
Does the empty string('') mean that it doesn't parse the id correctly from the URL? Isn't my url pattern correct?
EDIT: Damn my eyes....needed to put \d+ and the image reg pattern inside the parentheses
url(r'^xray/(?P<id>\d+)/(?P<image_name>\w+\.\w{3})$,...)
Sorry for posting a question...(How do i delete my own questions?)
The correct url is:
url(r'^xray/(?P<id>\d+)/(?P<image_name\w+\.\w{3})$', mypil.views.edit_xray)
You should put the pattern in the parentnesses.
ImageField on the model
some_image = models.ImageField(upload_to= get_upload_path)
This is what i use for serializing:
data = serializers.serialize("json",ModelName.objects.all(),use_natural_keys=True)
the json i get for some_image is relative to the MEDIA_URL
to solve it manualy i call some_image.url, and that works fine, how do i make it so this will be the default value that the image returns when using serializers.serialize
Here are the docs for specifying which fields get serialized: https://docs.djangoproject.com/en/dev/topics/serialization/#subset-of-fields
It sounds like you should include url and all the other fields except some_image.
I was trying to find a proper solution too. But eventually came up with following code:
stores = list(Store.objects.all().values('title', 'logo'))
for store in stores:
store['logo'] = \
request.build_absolute_uri(settings.MEDIA_URL+store['logo'])
stores_json = json.dumps(stores)
In Django I have my Site model that contains the field "base_url" that's the base url of the site. I've an object like this:
foo = Site(base_url="http://foo_base_url.com/")
foo.save()
I receive an url and I want to obtain the site object having this url. I would like to perform a query in django like this:
Site.objects.get(base_url__is_substring="http://foo_base_url.com/something_non_base_url")
How can I perform this query?
Thanx
edit:
It doesn't exist a pattern for base_url, my foo site can be:
foo = Site(base_url="http://foo.com/base/url/")
What you want is not provided by the Django ORM but you can use the where param described under the reference for QuerySet:
url = "http://foo_base_url.com/something_non_base_url"
Site.objects.extra(where=["%s LIKE CONCAT('%%',field,'%%')"], params=[url]).get()
Keep in mind that there is no standard method of concatenation across DMBS so if you migrate, you'll have to migrate this code.
The only portable method would be to filter it using Python:
sites = [site for site in Site.objects.all() if site.base_url in url]
although this is of course not ideal for huge data sets.
I use django-tables2 to show some data in page,and now I want to make the cell link to some url,but the link url such as :
url(r'^(?P\w+)/(?P\d+)/$', 'pool.views.pooldatestock',
name="pool_date_stock"),
and I read the documents of django-tables2,but I can't find some excample about this problem.
the tables show in the page's url just like:http://127.0.0.1:8000/pool/20111222/
I try to write this in my tables.py :
class PoolTable(tables.Table):
number = tables.LinkColumn('pool.views.pooldatestock', args=[A('number')])
date = tables.Column()
and then I try to write:
class PoolTable(tables.Table):
number=tables.LinkColumn('pool.views.pooldatestock',
args=[A('date')],
kwargs=A('number')])
date = tables.Column()
but it's error too...
somebody can tell me how to solve this problem?or should I create my own table view, without django-tables.
Thanks.and Merry Christmas:)
It makes no sense for the kwargs parameter to be given a list, it should be given a dict. However as your URL doesn't used named groups, it doesn't need keyword arguments anyway. Just put both URL parameters in the args parameter:
class PoolTable(tables.Table):
number = tables.LinkColumn('pool.views.pooldatestock',
args=[A('date'), A('number')])
date = tables.Column()