These are chat archives for django/django

13th
Jun 2018
Lefort
@PabloLefort
Jun 13 2018 11:10
@LukeVideo its seems rare to me passing the form in to the context, it is not the Django way, a little bit hacky
can you refact that?
@wmmrunboo read the docs, and please use Python 3.x and Django 2.x https://docs.djangoproject.com/en/2.0/
Luke-VideoLibre
@LukeVideo
Jun 13 2018 12:19
@PabloLefort Yeah i'm on it. I realized it was pretty hacky when comparing to the documentation... Thanks. I'll probably get messages working if i clean this mess up. ;)
Lefort
@PabloLefort
Jun 13 2018 12:20
yes!
your welcome dude
prinxy
@prinxy
Jun 13 2018 12:26
Hello guys while trying to run test in my django app, i get this erro
Creating test database for alias 'default'... Got an error creating the test database: (1044, "Access denied for user 'admin'@'localhost' to database 'test_admin'")
how can i resolve this...

What i have done

-- i tried adding

 'TEST':{
        'NAME' : 'test_admin',
}

to settings.py but still no positive result

Lefort
@PabloLefort
Jun 13 2018 12:28
@prinxy try removing test configuration database
prinxy
@prinxy
Jun 13 2018 12:28
i've still no positive result
Lefort
@PabloLefort
Jun 13 2018 12:28
that will use the same user that you'r using with the non-test database
all right
you have access to the database?
prinxy
@prinxy
Jun 13 2018 12:29
yes..
Lefort
@PabloLefort
Jun 13 2018 12:29
what db are you using?
prinxy
@prinxy
Jun 13 2018 12:29
MySQL
Lefort
@PabloLefort
Jun 13 2018 12:30
try this: GRANT ALL PRIVILEGES ON test_admin.* TO 'admin'@'localhost';
prinxy
@prinxy
Jun 13 2018 12:30
i have also granted db user all privileges still to no good end
Lefort
@PabloLefort
Jun 13 2018 12:31
hmm
rare
have you check privileges?
login with that user to mysql and make some query
if that goes well its a django problem
prinxy
@prinxy
Jun 13 2018 12:32
ok
I just created a test table with db user and it work perfectly well
i ran migrations & migrate and all of my models were also created to the database

hello @PabloLefort , can my test structuring cause it...

i'm working on a large project, so i don't want to use the tests.py that comes by default
so i created a folder named test and within it i created a test_whatever.py file to run all of my tests

( can this be the cause ) ?

Lefort
@PabloLefort
Jun 13 2018 12:37
awesome!
did you remove the test.py?
prinxy
@prinxy
Jun 13 2018 12:38

File Structure

     -- app
         -- test
             -- test_whatever.py
yes i removed test.py
Lefort
@PabloLefort
Jun 13 2018 12:38
rename test folder as tests
Chris Goodwin
@hannylicious
Jun 13 2018 12:39
You have to set the USER in your TEST settings I believe
it's trying to use admin it looks like
prinxy
@prinxy
Jun 13 2018 12:39
@PabloLefort i renamed it same issue
Chris Goodwin
@hannylicious
Jun 13 2018 12:39
USER: test_admin
Lefort
@PabloLefort
Jun 13 2018 12:40
@hannylicious @prinxy already resolved the db issue
Chris Goodwin
@hannylicious
Jun 13 2018 12:40
Ah, gotcha.
Lefort
@PabloLefort
Jun 13 2018 12:40
@prinxy ok, and you have an __init__.py inside the tests folder?
prinxy
@prinxy
Jun 13 2018 12:40
no
@PabloLefort
Lefort
@PabloLefort
Jun 13 2018 12:41
add it! :muscle:
and try again
prinxy
@prinxy
Jun 13 2018 12:41
same issue @PabloLefort
i'm running my test with coverage run manage.py test app_name
i've also tried python manage.py test app_name
Chris Goodwin
@hannylicious
Jun 13 2018 12:42
We use a similar structure as you --app --tests --test_whatever.py - try renaming your directory to tests, not test
prinxy
@prinxy
Jun 13 2018 12:43
i've @hannylicious
Lefort
@PabloLefort
Jun 13 2018 12:43
app_name is on INSTALLED_APPS?
prinxy
@prinxy
Jun 13 2018 12:43
yes
@PabloLefort
Lefort
@PabloLefort
Jun 13 2018 12:44
from django.conf import settings
print(settings.INSTALLED_APPS)
can you check it again?
prinxy
@prinxy
Jun 13 2018 12:44
just checked and app is installed
Lefort
@PabloLefort
Jun 13 2018 12:45
perfect
and if you run python manage.py test what is the output?
prinxy
@prinxy
Jun 13 2018 12:46
same error
Chris Goodwin
@hannylicious
Jun 13 2018 12:46
What is the error exactly?
prinxy
@prinxy
Jun 13 2018 12:48
Got an error creating the test database: (1044, "Access denied for user 'admin'@'localhost' to database 'test_dbName
@hannylicious
Chris Goodwin
@hannylicious
Jun 13 2018 12:49
Are you using the keepdb flag?
prinxy
@prinxy
Jun 13 2018 12:49
no
Lefort
@PabloLefort
Jun 13 2018 12:50
try to restart mysql
Chris Goodwin
@hannylicious
Jun 13 2018 12:51
So, Django wants to create an entirely new 'test_db' automatically - if it doesn't have access to this, you can create it manually and then use the --keepdb flag (which is my understanding of it) - but otherwise Django needs rights to create databases (not just tables) on the system
prinxy
@prinxy
Jun 13 2018 12:52
@PabloLefort same issue
Chris Goodwin
@hannylicious
Jun 13 2018 12:52
I ran into this issue recently where we use MSSQL and Django wants to touch the Master db when creating a test_dbName database.... but our DB Admin said he will not give access to it. So... we had to shelve that idea and run tests a different way.
prinxy
@prinxy
Jun 13 2018 12:53
--keepdb why this flag ?
Chris Goodwin
@hannylicious
Jun 13 2018 12:54
Because it tells Django "just use test_dbname" and keeps the db between tests (doesn't recreate it when running tests or tear it down after they've ran)
Lefort
@PabloLefort
Jun 13 2018 12:54
--keepdb its for run tests faster, it keeps some data
prinxy
@prinxy
Jun 13 2018 12:54
ok
@hannylicious @PabloLefort
Lefort
@PabloLefort
Jun 13 2018 12:55
@prinxy if you connect to mysql with the admin user, can you create a database?
prinxy
@prinxy
Jun 13 2018 12:55
let me create the test_dbName and see if it'll do some good @hannylicious
@PabloLefort yes
Lefort
@PabloLefort
Jun 13 2018 12:56
hmm rare
Chris Goodwin
@hannylicious
Jun 13 2018 12:57
My understanding is you can pre-create the test_dbName (and use --keepdb); or you can give django admin rights to create new DB's on the mySQL instance - I think either would work although the latter would probably be most appropriate (depending on your DB admin and contents of the DB)
prinxy
@prinxy
Jun 13 2018 12:59
i created a test_dbName, it seems like i also have to grant access / privileges to admin@localhost for that db. right @hannylicious ?
Chris Goodwin
@hannylicious
Jun 13 2018 13:01
Yeah. You'd want it to have the ability to do things there
prinxy
@prinxy
Jun 13 2018 13:01
it work
test ran
Thanks @hannylicious and @PabloLefort
Chris Goodwin
@hannylicious
Jun 13 2018 13:02
Awesome. Nice work!
prinxy
@prinxy
Jun 13 2018 13:02
@hannylicious will django automatically tearDown the records made to the database ?
after test completes
Chris Goodwin
@hannylicious
Jun 13 2018 13:02
Not using --keepdb
at least, that's my understanding.
You can just check thought, it might tearDown the records it makes but just leave the DB instance up
prinxy
@prinxy
Jun 13 2018 13:03
ok, bcus it wouldn't be good to populate a testing db with actual test records..
Chris Goodwin
@hannylicious
Jun 13 2018 13:04
Yeah, I just played around a bit with that method because of a really niche setup we have going on currently. So I didn't have a chance to see if it keeps the records in the testDB or if it tears them down. I would think that it tears them down.
prinxy
@prinxy
Jun 13 2018 13:06
it tears it down automatically without the --keepdb
i logged into mysql and i show databases and test_dbName was no where to be found
but my test are still running as desired
@hannylicious
Chris Goodwin
@hannylicious
Jun 13 2018 13:07
Alright, well looks like you're full steam ahead then, no?
Lefort
@PabloLefort
Jun 13 2018 13:07
awesome :rocket: !
prinxy
@prinxy
Jun 13 2018 13:08
lots of thanks @hannylicious and @PabloLefort
Lefort
@PabloLefort
Jun 13 2018 13:08
no problem :smile:
Chris Goodwin
@hannylicious
Jun 13 2018 13:09
Anytime!
garynobles
@garynobles
Jun 13 2018 13:34

I'm in my 2nd week of Django, thrown in the deepend as usual.

I'm on to Foreign keys, I have them working and it shows up on my template as a dropdown, all is lovely except it displays Location object(1) etc. using:
{{ form.location_id}}
I tried adding
{{ form.location_id.location_name}}
and the form element does not render, any ideas?

Lefort
@PabloLefort
Jun 13 2018 13:36
how are you passing the object to the form? @garynobles
garynobles
@garynobles
Jun 13 2018 13:38
@PabloLefort Heres the entire block
{% extends 'container/base.html' %}
{% load widget_tweaks %}
{% block content %}
<div class="container">
    <h1>Container</h1>
    <div class="col-sm-3">
    <form method="POST" class="post-form">{% csrf_token %}
        <!--  {{ form.as_p }}   -->
        Container Name{{ form.container_name|add_class:"form-control"}}<br />
        Container Type{{ form.container_type|add_class:"form-control"}}<br />
        Location ID{{ form.location_id|add_class:"form-control"}}<br />
        Sample ID{{ form.sample_id|add_class:"form-control"}}<br />
        Current Location TMP{{ form.current_location_tmp|add_class:"form-control"}}<br />
        <button type="submit" class="save btn btn-default">Save</button>
    </form>
  </div>
</div>
{% endblock %}
Lefort
@PabloLefort
Jun 13 2018 13:47
and what is the output of {{ form }}?
garynobles
@garynobles
Jun 13 2018 13:49
I expect it is the same just without the formating...
yes, same functionality
So form.location_id is a ForeignKey set coming from the containers table, it is referencing location_id in the location table, the Key set is displayed, as location object(1), location object(2) etc, the Location_id (an integer) should be saved to the database but I would like to display something a little more meaningfull like the location_name
Chris Goodwin
@hannylicious
Jun 13 2018 13:52
on the Location model, couldn't you create something like def __str__(self): return self.name or something similar?
Lefort
@PabloLefort
Jun 13 2018 13:53
yup, or you can use template tags
but, a more django way is to pass the object with the view
garynobles
@garynobles
Jun 13 2018 14:02
I tried def __str__(self): return self.location_name with {{ store.location_name }} but nothing showed up
I thought the way to traverse a FK was to add .location_name to the end of form.location_id or am I getting confused?
Chris Goodwin
@hannylicious
Jun 13 2018 14:16
if the str is set on the model, you shouldn't need to do {{ store.location_name }}, rather you could just do {{ store }} and it would return the location_name
Lefort
@PabloLefort
Jun 13 2018 14:16
awesome
:clap:
Chris Goodwin
@hannylicious
Jun 13 2018 14:17
enjoy the read :smile:
garynobles
@garynobles
Jun 13 2018 14:17
thanks
Lefort
@PabloLefort
Jun 13 2018 14:17
all ways good
no problem dude! :smile:
garynobles
@garynobles
Jun 13 2018 14:19
works perfectly :smile:
Chris Goodwin
@hannylicious
Jun 13 2018 14:23
Cool, glad you're set!
:shipit:
garynobles
@garynobles
Jun 13 2018 14:24
one little thing extra... adding orderby? This didn't work, it was a guess
def __str__(self): return self.location_name.orderby('location_name')
or would it be orderby(self)
Luke-VideoLibre
@LukeVideo
Jun 13 2018 14:24

I got rid of context and managed to get my 2 articles on my home page.

class Home(generic.TemplateView):
    model = Article
    template_name = 'pages/home.html'
    initial = {'key': 'value'}
    form_class = ContactForm
    latest_articles = Article.objects.order_by('-date')[:2]


    def get(self, request, *args, **kwargs):
        form = self.form_class()
        latest_articles = self.latest_articles
        storage = get_messages(request)
        return render(request, self.template_name, {'form': form, "latest_articles":latest_articles, "message":storage})


    def post(self, request, *args, **kwargs):
        form = self.form_class(initial=self.initial)
        storage = get_messages(request)
        print(storage)
        if form.is_valid():
            messages.succes(request,'Message sent.')
            storage = get_messages(request)
            return HttpResponseRedirect('/')
        latest_articles = self.latest_articles
        return render(self.request, self.template_name, {'form': form, "latest_articles":latest_articles,  "message":storage})

But messages still don't show up and sessionStorage is empty in the browser...

Lefort
@PabloLefort
Jun 13 2018 14:31
on the get try to add a message and see if it renders
Luke-VideoLibre
@LukeVideo
Jun 13 2018 14:55
No luck.
Chris Goodwin
@hannylicious
Jun 13 2018 15:02
@garynobles is it a model form?
garynobles
@garynobles
Jun 13 2018 15:09
@hannylicious I believe so, yes
Chris Goodwin
@hannylicious
Jun 13 2018 15:09
@garynobles The ordering is going to happen on your queryset that you're using. You're not going to set the ordering in that __str__ method. That method simply tells it how to display the model instance in string form ( if I'm saying that properly) for ordering - you would do that on your form model something like this would work - although I dislike the idea of adding an 'ordering field' as recommended in the accepted answer. But it shows you how/where to use the order_byfeature
So probably something like this:
my_model_form_field = forms.ModelChoiceField(
    queryset=Location.objects.all().order_by('location_name'),
    widget=forms.Select())
garynobles
@garynobles
Jun 13 2018 16:38
My final problem of today, for some reason the store url stops working and the detailcontainer throws a 404, if I get them working then the store does nothing when I click the corresponding hyperlink, have I made an obvious error?
urlpatterns = [
    #locations
    url(r'^location/$', views.alllocation, name='alllocation'),
    path('<int:location_id>/', views.detaillocation, name='detaillocation'),

    url(r'^createlocation/$', views.createlocation, name='createlocation'),
    url(r'^edit/(?P<pk>\d+)/edit/$', views.editlocation, name='editlocation'),

    #container
    url(r'^container/$', views.allcontainer, name='allcontainer'),
    path('<int:container_id>/', views.detailcontainer, name='detailcontainer'),

    url(r'^createcontainer/$', views.createcontainer, name='createcontainer'),
    url(r'^edit/(?P<pk>\d+)/edit/$', views.editcontainer, name='editcontainer'),

    #store
    url('', views.allstore, name='allstore'),
    path('<int:store_id>/', views.detailstore, name='detailstore'),

    url(r'^createstore/$', views.createstore, name='createstore'),
    url(r'^edit/(?P<pk>\d+)/edit/$', views.editstore, name='editstore'),
]
prinxy
@prinxy
Jun 13 2018 16:56
hello all, i created a custom user model in which i also created a custom UserManager object objects = UserManager in my models.
when i run python manage.py shell and import my User class and do something like User.objects.all( ) everything works perfectly fine including User.objects.create( )
but when testing that same User model, i did User.objects.create( ) and i get an error saying AttributeError: type object User has no attribute objects
Please guys i've been stucked on this since noon...
garynobles
@garynobles
Jun 13 2018 17:03
With this configuration all the links work except the store which I click but nothing appears to happen
urlpatterns = [
    #locations
    url(r'^location/$', views.alllocation, name='alllocation'),
    path('location/<int:location_id>/', views.detaillocation, name='detaillocation'),

    url(r'^createlocation/$', views.createlocation, name='createlocation'),
    url(r'^location/edit/(?P<pk>\d+)/edit/$', views.editlocation, name='editlocation'),

    #container
    url(r'^container/$', views.allcontainer, name='allcontainer'),
   path('container/<int:container_id>/', views.detailcontainer, name='detailcontainer'),

    url(r'^container/createcontainer/$', views.createcontainer, name='createcontainer'),
    url(r'^container/edit/(?P<pk>\d+)/edit/$', views.editcontainer, name='editcontainer'),

    #store
    url('store/', views.allstore, name='allstore'),
   path('store/<int:store_id>/', views.detailstore, name='detailstore'),

    url(r'^store/createstore/$', views.createstore, name='createstore'),
    url(r'^store/edit/(?P<pk>\d+)/edit/$', views.editstore, name='editstore'),
]
prinxy
@prinxy
Jun 13 2018 17:07
do you mean views.allstore or all of the links below # store
@garynobles
garynobles
@garynobles
Jun 13 2018 17:10
the allstore link works, but then I click on the next link (detailstore) and nothing happens, the create/editstore sits under there so no idea if they are working normally @prinxy
the links above #store only behave with the prefixes, e.g. container/<int:container_id> , location/edit, etc
without the prefixes the links go to the same place e.g. store/<a number>
prinxy
@prinxy
Jun 13 2018 17:13
i'm not too familiar with path, as i'm working with django 1.11. if views.detailcontainer link is working then it means the path pattern for views.detailstore is also correct
Have you check the template rendering that path pattern if the syntax is well formatted ?
garynobles
@garynobles
Jun 13 2018 17:17
here it is
  <div class="row">
    {% for store in store.all %}
    <div class="col-md-4">
      <a href="{% url 'detailstore' store.store_id %}">
        <div class="card mb-4 box-shadow">
          <img class="card-img-top" src="" />
          <div class="card-body">
            <p class="card-text text-center">{{ store.store_name }}</p>
          </div>
        </a>
      </div>
    </div>
    {% endfor %}
in the views.py
def allstore(request):
    store = Store.objects
    return render(request, 'store/allstore.html', {'store':store})

def detailstore(request, store_id):
    detailstore = get_object_or_404(Store, pk=store_id)
    return render(request, 'store/detailstore.html', {'store':detailstore})
prinxy
@prinxy
Jun 13 2018 17:18
have you tried appname:detailstore
instead of just detailstore
garynobles
@garynobles
Jun 13 2018 17:22
that doesn't seem to help
It seems strange some work and some don't
prinxy
@prinxy
Jun 13 2018 17:26
@garynobles instead of using store.store_id i guess it should be store.id. You should give this a shot...
check you template
garynobles
@garynobles
Jun 13 2018 17:29
that wont be it, i'm running django with a postgres backend
so I know store_id is a column
prinxy
@prinxy
Jun 13 2018 17:30
you are trying to address the primary key for that link to work....
if you want the store of id 1 your url will be store/1
that 1 uniquely identifies the store
except you made the store_id primary key, to my best understanding that should be the only exception
Lefort
@PabloLefort
Jun 13 2018 17:32
@garynobles i think it is a better approach to use store.id as said by @prinxy because you dont have to think on the database
garynobles
@garynobles
Jun 13 2018 17:33
store_id = models.AutoField(primary_key=True)
Lefort
@PabloLefort
Jun 13 2018 17:34
do you really need it? Django does that by default
but as id
garynobles
@garynobles
Jun 13 2018 17:35
I'm a backend developer, new to the Django frontend, I'm mapping to an already established database, so there is no discussion there. @PabloLefort
yes the url should be store/1 if it has to be store/store/1 to work that is ok too
the other urls are store/container/1 etc
prinxy
@prinxy
Jun 13 2018 17:37
@garynobles when you click on the link, does it throw any error ?
garynobles
@garynobles
Jun 13 2018 17:37
it does nothing, its inactive
or it is reloading the page, but thats hard to tell
prinxy
@prinxy
Jun 13 2018 17:38
that means it's like clicking a link with href="#"... wow
garynobles
@garynobles
Jun 13 2018 17:38
exactly
Chris Goodwin
@hannylicious
Jun 13 2018 18:03
@garynobles if you hover over the link - what does it show? Or if you inspect the link in the browser - does it show the correct href?
garynobles
@garynobles
Jun 13 2018 18:21
I'm not sure what I've done, but now it works...
I've made a backup!
Chris Goodwin
@hannylicious
Jun 13 2018 18:31
Don't forget to use version control :) then you'll know what you did!
avinash bharti
@avinashbharti97
Jun 13 2018 19:52
Hello everyone,
I am working on a Django webapp which uses mongoengine (Mongodb as database).
I want to create an User registration app, I know Mongoengine provide auth backend but I want to know if any example or tutorial available for User registration using Mongoengine
Thank you :)
Adole Samuel
@int-elligentSam
Jun 13 2018 20:45
is there anyway to keep track of user login details for each day by getting timestamp and login and logout count with their timestamps? please how would this be represented?