These are chat archives for django/django

1st
Jun 2016
Ramon Moraes
@vyscond
Jun 01 2016 00:58
Hey guys. Is there a way to set an --auto-confirm flag on the south app when running migrate? (using django 1.3 here)
Oleksiy
@doubledare704
Jun 01 2016 07:21
Hello, guys, I have an issue with django uploaded files
in settings i have
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls
if DEBUG:
    # serve files from media folder
    urlpatterns += patterns('',
                            url(r'^media/(?P<path>.*)$',
                                'django.views.static.serve',
                                {'document_root': MEDIA_ROOT}),
                            )

models

class Item(models.Model):
    name = models.CharField(max_length=255, verbose_name="Названия товара")
    price = models.IntegerField(default=0, verbose_name="Цена")
    image = models.ImageField(verbose_name="Фото", upload_to='item')
    alias = models.SlugField(verbose_name="alias  категория")

template

{% for item in tovars %}
            <div class="product-image-wrapper" style="float:left;height:320px;width:150px;margin-left:40px;">
                <br/>
                <div class="single-products">
                    <div class="productinfo text-center">
                        <img class="img-product" src="{{ item.image.url }}" alt=""/>
                        <h2 id="{{ item.id }}" class="value">{{ item.price }}</h2>
                        <p class="name">{{ item.name }}</p>
                        <a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart ">
                        </i>Add to cart</a>
                    </div>


                </div>
            </div>
        {% endfor %}

view

def item(request, alias):
    try:
        tovar = Item.objects.get(alias=alias)
    except Item.DoesNotExist:
        raise Http404('Not Found ')
    context = {

        'tovar': tovar,

    }
    return HttpResponse(render_to_string("item.html", context))

in that page it returns 500 and no image

blob
Oleksiy
@doubledare704
Jun 01 2016 07:26
this image really stored in that folder media/item
blob
blob
console returns this
Denis Ivanov
@tetafro
Jun 01 2016 07:57
# serve files from media folder
    urlpatterns += patterns('',
                            url(r'^media/(?P<path>.*)$',
                                'django.views.static.serve',
                                {'document_root': MEDIA_ROOT}),
                            )
should url be here?
on the 3rd line. i think it should be just tuple, not url() call
Oleksiy
@doubledare704
Jun 01 2016 07:59
I've changed to this construction, same thing with 500 error
urlpatterns += patterns('',

                        ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Denis Ivanov
@tetafro
Jun 01 2016 08:01
you don't need patterns('', ) here
try just
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Oleksiy
@doubledare704
Jun 01 2016 08:02
so, i should write only urlpatterns += static(...)?
ok
it returns the same error in console and in browser
Denis Ivanov
@tetafro
Jun 01 2016 08:12
no more ideas, sorry)
Bernard Parah
@b3h3rkz
Jun 01 2016 13:12
Hi guys, I am stuck and will appreciate any help on the way forward.
I am working on a car reservation system, with three models, Car, Reservation and User
I am using a generic createview and modelform to book car. How do I pass the currently selected car into the form so the user doesnt have to select a car while filling the form?
I saw things like storing it in a session or using get init
Kinda stuck on which way to go. Logic is mainly the problem here that's why I didn't drop any snippets
Roman
@roman-oxenuk
Jun 01 2016 13:41
How does user select a car, at first?
How you planning to organize your UI?
Bernard Parah
@b3h3rkz
Jun 01 2016 14:04
there's a page where cars are displayed
I am assuming the user is already signed in
select a car from the page
User is then taken to another page with the form reservation form displayed with the car passed into the url
I think this will make it clearer as I am trying to mimmick this in django
That's exactly the flow I am trying to follow
lego
@legobillyjoe
Jun 01 2016 15:21
@byteknacker go.cd
John Wu
@byteknacker
Jun 01 2016 15:25
@legobillyjoe thanks that looks amazing
Roman
@roman-oxenuk
Jun 01 2016 15:32

@b3h3rkz
I think you can make an AJAX request in car selecting page to store selected car in session. See the examples here:
https://docs.djangoproject.com/en/1.9/topics/http/sessions/#examples

And then just override get_initial method in your view responsible for form showing to get selected car from session and pass it to the form like initial value. Here is a sample code:

def get_initial(self):
    initial = self.initial.copy()
    selected_car = self.request.session.get('selected_car', None)
    if selected_car:
        initial.update({'selected_car': selected_car})
    return initial