These are chat archives for django/django

1st
Dec 2016
Carlos Azuaje
@CharlyJazz
Dec 01 2016 01:43
What is the best way to control my view of like_longboard , I'm using ajax and I want to prevent users from clicking on the same product and generating more than 1 "like" ?
def like_longboard(request):
    longboard_id = request.GET.get('longboard_id', None)

    likes = 0
    if (longboard_id):
        longboard = Longboard.objects.get(id=int(longboard_id))
        if longboard is not None:
            likes = longboard.likes + 1
            longboard.likes = likes
            longboard.save()
    return HttpResponse(likes)
Michael
@IrSent
Dec 01 2016 06:58
last time I did it I used many-to-many field
users_like = models.ManyToManyField(
    Profile,
    related_name='question_liked',
    blank=True
)
on the thing that I wanted to get likes for
aaaand an api call that would be received on "like”-request:
@api_view(['POST'])
@permission_classes((IsAuthenticated, ))
def question_like(request, pk):
    if request.method == 'POST':
        try:
            question = Question.objects.get(pk=pk)
        except Question.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        profile = Profile.objects.get(user=request.user)
        if profile not in question.users_like.all():
            question.users_like.add(profile)
        else:
            question.users_like.remove(profile)
        return Response(status=status.HTTP_200_OK)
@CharlyJazz
Dennis Decoene
@DDecoene
Dec 01 2016 09:49
Hello, has anyone had this: I run a django app on windows, started as a service that I made with SRVMAN. I installed miniconda2 and the exec of the service is: C:\Program Files\Miniconda2\envs\test\python.exe c:\djtest\webinterface\manage.py runserver localhost:8585. For some reason I get an error that it does not find the template “registration/login.html” which is in c:\djtest\templates.
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ["templates"],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
and
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:

    django.template.loaders.filesystem.Loader: C:\Windows\system32\templates\registration\login.html (Source does not exist)
    django.template.loaders.app_directories.Loader: C:\Program Files\Miniconda2\envs\reverse_edi\lib\site-packages\django\contrib\admin\templates\registration\login.html (Source does not exist)
    django.template.loaders.app_directories.Loader: C:\Program Files\Miniconda2\envs\reverse_edi\lib\site-packages\django\contrib\auth\templates\registration\login.html (Source does not exis
it is curious (to me at least) as to why the filesystem loader looks at system32
Michael
@IrSent
Dec 01 2016 09:53
try:
TEMPLATES = [
    …
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    ...
]
Dennis Decoene
@DDecoene
Dec 01 2016 09:55
yes that did the trick. somehow I always seem to mess up the template path part of the django app.
Thank you enormously!!!! you just saved my day!!!
Michael
@IrSent
Dec 01 2016 09:56
I had troubles with this issue too.
Dennis Decoene
@DDecoene
Dec 01 2016 09:58
that and static files give me headaches :D
Michael
@IrSent
Dec 01 2016 10:00
and maybe that’s why it is strongly recommended not to use static files feature in production
Pami Ketolainen
@keto
Dec 01 2016 10:11
@IrSent who recommends that?
and what would be the alternative for the static files feature?
dedicated server or cdn
Pami Ketolainen
@keto
Dec 01 2016 10:20
well, yes, obviously not serving the static files via django, but using the static files feature is not in anyway not recommended
Dennis Decoene
@DDecoene
Dec 01 2016 10:22
no I don’t use it in production but for testing/dev. Yet it always give me troubles getting it running.
Pami Ketolainen
@keto
Dec 01 2016 10:24
I would say that the point of the static files feature is to make it easy to both deploy them properly in production and use them in development with runserver
Simon Frid
@fridiculous
Dec 01 2016 17:46
does anyone have good practices for importing settings from apps into a main project
in particular, configuring settings in a particular order. So that settings from kwargs > main project > default settings for app
Michael
@IrSent
Dec 01 2016 18:59
why you would even want to do something like that?
Alex10Alex
@Alex10Alex
Dec 01 2016 19:53
подскажите, django выдает ошибку : File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
File "/home/alex/seven/src/seven/urls.py", line 22, in <module>
url(r'^$', profiles.views.home, name = 'home'),
NameError: name 'profiles' is not defined

@Alex10Alex
from . import profiles
не работает
from django.conf.urls import url, include
from django.contrib import admin
from . import profiles

urlpatterns =[
url(r'^$', profiles.views.home, name = 'home'),
url(r'^admin/', admin.site.urls),
]
вот мой views.py

Philip Eckert
@pae4557
Dec 01 2016 20:36
So I'm working on a project a friend started, and he didn't implement the database when he should have.
I need to save model instances, to be later identified by their username. My code is as follows:
username = User.get_username()
        hydroInstance = HydroGraph(name=username, static_Pressure=statp, residual_Pressure=resP, supply_Flow=resFlow, 
            required_Pressure=reqP, required_Flow=reqFlow, elevation=elev, safety_Factor=safe, hose_Requirement=hose, 
            pump_Type=sourceSituation)
        hydroInstance.save()
I thought this was the proper way to save to the database, but when I check under the model in the admin page, I don't see any new models saved. Am I doing something wrong?
Simon Frid
@fridiculous
Dec 01 2016 21:04
@IrSent because i'm trying to support a standalone app as well as an extension
Jimmy Merrild Krag
@beruic
Dec 01 2016 21:17
@pae4557 Why doesn't HydroGraph use a foreign key to the user instead? Or am I misunderstanding something? Isn't User supposed to be a user instance?
Philip Eckert
@pae4557
Dec 01 2016 21:32
@beruic hmm you're right
@beruic username = request.user.get_username()
using that instead, it still gives me a value error that the pk doesn't exist, but I thought the pk was supposed to be there automatically, and only be removed if you specifically override that in the model?
@beruic Like .save() just does an INSERT instead of UPDATE so I forced it, and it just tells me that the PK doesn't exist :/