These are chat archives for django/django

15th
Nov 2016
rishu-18
@rishu-18
Nov 15 2016 02:19
Hii #Isamel Veneers
.. you can use like this models.object.get(username=,I'd=) and if it is not null send to ur webpage and as context or as Jain object and display there.
Jason object
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 03:55
Thank you @rishu-18 !
Tushar Joshi
@tusharvjoshi
Nov 15 2016 05:24
I want to list out steps for migrating a Rails site to Django site. Are there any guides or pages which mention this? What can be useful pointers for making such a list. The site in question is a fairly complex Rails site which needs to be migrated/converted into Django site
Miracle Ayodele
@iammiracle
Nov 15 2016 08:21
hello guys, hw do i make the authenticate function read my username and password from my database, because my authenticate function returns None here
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:33
Miracle Ayodele
@iammiracle
Nov 15 2016 08:33
yes @RadoRado
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:33
well
can you open a shell with python manage.py shell
and query the user you are trying to login
Miracle Ayodele
@iammiracle
Nov 15 2016 08:34
i ve done dt
nd it returns true
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:34
from django.contrib.auth.models import User
user = User.objects.get(id=some_id) # or email or username
user.password
do you see it as plain text?
user.check_password(the_password)
also
True or False?
Miracle Ayodele
@iammiracle
Nov 15 2016 08:36
True
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:36
what's the value of user.password
then, try in the shell to authenticate that particular user
Miracle Ayodele
@iammiracle
Nov 15 2016 08:37
u'pbkdf2_sha256$30000$NrsNweFRgN22$c9A7SPxx6Tb7ExzoVylGpsi/K09fS/g2FTtdR8P00J8='
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:37
good
from django.contrib.auth import authenticate
and try to see the result
Miracle Ayodele
@iammiracle
Nov 15 2016 08:39
print user
nd it returns none
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:39
authenticate(username='email_here', password='asdf')
Miracle Ayodele
@iammiracle
Nov 15 2016 08:40
yeah
i guess the first try is linked with the superuser
nd dt is why it is returning true
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:41
so have you changed your auth model to use email?
because the original kwarg for query is username and you are passing email
Also have you made any changes to the authentication backends?
Miracle Ayodele
@iammiracle
Nov 15 2016 08:42

u mean
AUTHENTICATION_BACKENDS = (

'django.contrib.auth.backends.ModelBackend',

)

Radoslav Georgiev
@RadoRado
Nov 15 2016 08:43
yep
any changes to AUTH_USER_MODEL
Miracle Ayodele
@iammiracle
Nov 15 2016 08:46
noo
i dont have AUTH_USER_MODEL
or what do u mean by that
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:47
OK, so try the authenticate by passing username, not email as keyword argument
and the username of the user as value
authenticate(username=user.username, password='asdf')
Miracle Ayodele
@iammiracle
Nov 15 2016 08:49
the user is not recognized
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:49
what do you mean by that?
the user from the previous query
Miracle Ayodele
@iammiracle
Nov 15 2016 08:50
re u still talking about the shell?
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:50
yes
we haven't left the shell yet :D
Miracle Ayodele
@iammiracle
Nov 15 2016 08:51
aiit
the user.username hw does it work
i am new to this, so sorry if i ask too much
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:53
you managed to authenticate?
Miracle Ayodele
@iammiracle
Nov 15 2016 08:53
i did
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:53
OK
so
you are using Django's User model for authentication
Miracle Ayodele
@iammiracle
Nov 15 2016 08:54
yea
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:54
which requires username nad password - this is the default behavior
If you want to make the default "username" to be email
Miracle Ayodele
@iammiracle
Nov 15 2016 08:55
still returns none
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:55
because this is the email, not the username
in the default model, email is not part of the authentication scheme
Miracle Ayodele
@iammiracle
Nov 15 2016 08:55
ok
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:55
Read the above docs
Miracle Ayodele
@iammiracle
Nov 15 2016 08:55
aiit
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:56
and you will have more understanding about that
also
If you are not supporting legacy projects
ditch Python 2 and use latest
Python 3, Django 1.10
Miracle Ayodele
@iammiracle
Nov 15 2016 08:56
aiit bro
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:56
You can also check https://github.com/pydanny/cookiecutter-django for bootstraping projects
good luck :+1:
Miracle Ayodele
@iammiracle
Nov 15 2016 08:56
ok
so no special way of making sure the authenticate goes straight to my database
i am using mysql tho
Radoslav Georgiev
@RadoRado
Nov 15 2016 08:58
it is going to the database :)
Miracle Ayodele
@iammiracle
Nov 15 2016 08:58
lols
aiit
thanks bro
i will get back to you on the docs above
sukianto
@sukianto
Nov 15 2016 10:54
Hi, django generate it's own primary key, how if auto field end? will be empty?
what best can do for it?
from 1.10
sukianto
@sukianto
Nov 15 2016 11:04

@RadoRado ok thanks, I'm beginner code on django-rest-framework
Can u send me the best practice I can have, cause I'm code using same with DRF tutorial but someone review my code tell me:
"The code itself will not be easy to test due to its brittle and tightly coupled nature."
and my tests is so basic
"the tests for the database are tautological: is a an instance of A."

thanks for advance

Radoslav Georgiev
@RadoRado
Nov 15 2016 12:17
I'm not sure I got that
what's the problem that you have?
Maksym L
@maksymx
Nov 15 2016 15:48
Hi everyone! Due to this article https://godjango.com/blog/working-with-environment-variables-in-python/ , I want to ask you where do I need to set these environment variables if I use virtualenv? I've tried to set them in .bashrc but it gives no effect
Thanks
Hammad
@hammadzz
Nov 15 2016 15:59
Ill help you!
Miracle Ayodele
@iammiracle
Nov 15 2016 16:34

i really have big problem with this authentication.

I have read through https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#a-full-example but yet not authenticatiing, also tried https://docs.djangoproject.com/en/1.10/topics/auth/default/?#how-to-log-a-user-in the authentication returns None

Mark Heppner
@mheppner
Nov 15 2016 18:05
@yakudzam I recommend using Supervisor and you can either put them in your supervisor config or have them read from the user’s env variables
@koderNeptune you haven’t made any progress from the help @RadoRado provided you?
Miracle Ayodele
@iammiracle
Nov 15 2016 18:06
i have
Mark Heppner
@mheppner
Nov 15 2016 18:07
@koderNeptune then what are you asking?
Miracle Ayodele
@iammiracle
Nov 15 2016 18:08
its working yet
its not working yet
Mark Heppner
@mheppner
Nov 15 2016 18:09
@koderNeptune what isn’t working? please provide steps of what you’re trying instead of general statements
Miracle Ayodele
@iammiracle
Nov 15 2016 18:10
I want to be able to register user and the user should be able to login thats all
using my custom design
not django's
i have been able to register the user
but i cant login
authenticate function is returning None
Mark Heppner
@mheppner
Nov 15 2016 18:13
@koderNeptune no one will be able to help you without knowing what your custom design is. you’ve only posted your registration view
Miracle Ayodele
@iammiracle
Nov 15 2016 18:30
i posted the login view already

var code = "def user_login(request):

context = {}
if request.method == 'POST':
    print request.POST
    username = request.POST['username']
    password = request.POST['password']

    print username
    print password

    confirm_user = authenticate(username=username, password=password)

    print confirm_user
    if confirm_user is not None:
        if confirm_user.is_active:
            login(request, confirm_user)
            print (request.user.is_authenticated())
            print confirm_user
            return HttpResponseRedirect('/home/')
        else:
            print (request.user.is_authenticated())
            print confirm_user
            return HttpResponse("Your Account is disabled")
    else:
        print (request.user.is_authenticated())
        print "Invalid login details: {0}, {1}".format(username, password)
        return HttpResponse("Invalid login details supplied")
else:
    print (request.user.is_authenticated())
    return render(request, 'index.html', context)

";

but the authenticate function returns None
but from django reference i think i need to create a custom user authentication
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 18:37
Hello everyone!

According to this documentation on ListView:

What is the endpoint to access this page?

The urls.py is:

from django.conf.urls import url

from article.views import ArticleListView

urlpatterns = [
    url(r'^$', ArticleListView.as_view(), name='article-list'),
]
would it be localhost/article-list ?
Mark Heppner
@mheppner
Nov 15 2016 18:40
@Ismael-VC I think it would just be /
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 18:40
I also have:
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r"", include("log_auth.urls")),
    url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}),
    url(r'^logout/$', views.logout, {'next_page': '/login'}),
    url(r"^pacientes/", include("registros.urls")),   # <----- this line!
]
Instead of ArticleListView I changed to PatientsListView, but thats it
Mark Heppner
@mheppner
Nov 15 2016 18:42
@Ismael-VC Your URLs will get parsed in order. So whereever your ArticleListView is registered, if there is something above it that also matches /, it will never be reached.
@Ismael-VC is include(‘registros.urls’) pointing to the urls.py file that contains the url for PatientsListView?
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 18:43
Yes! It is
this is in registros/urls.py
from django.conf.urls import url

from registros.views import PacienteListView

urlpatterns = [
    url(r'^$', PacienteListView.as_view(), name='lista-pacientes'),
]
Then I include it in the urlpatterns of the project admin.py

I get this error:

RuntimeError: Model class registros.models.Person doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Mark Heppner
@mheppner
Nov 15 2016 18:46
@Ismael-VC so the matching URL you should access it through is /pacientes/. Your main urls.py is include()ing the registros/urls.py, all at the pacientes/ url. Anything in registros/urls.py file will be prefixed with pacientes/.
@Ismael-VC have you added ‘registros’ to INSTALLED_APPS in your settings.py?
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 18:47
Ohh I see
Mark Heppner
@mheppner
Nov 15 2016 18:48
@Ismael-VC So if you were to put url(r’extra/$’, some_other_view) in registros/urls.py, you would still access it through /pacientes/extra/.
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 18:53
Wow this is super complex :D
but I like it!
I fixed that error
Thank you very much!
Mark Heppner
@mheppner
Nov 15 2016 18:54

@koderNeptune if you’re not using a custom User model, then if you use authenticate() and it returns None, that means the authentication failed. You don’t need to create anything custom, you’re not even using a “custom design” at this point. Try this:

from django.contrib.auth.models import User
u = User.objects.create_user(‘myusername’, ‘fake@example.com’, ‘mypassword’)
u.save()

from django.contrib.auth import authenticate
valid_user = authenticate(username=‘myusername’, password=‘mypassword’)

valid_user should be the user you just made.

@Ismael-VC no problem. If you haven’t done so already, I suggest going through the 7 part tutorial. I haven’t looked at it in years, but I think they used to explain how the URL stuff works
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 19:11
Yes I'm reading it over and over again
I think I just need to keep getting practice on that
I have this view:
blob
with this template


<h1>Pacientes</h1>
<ul>
{% for paciente in object_list %}
    <li>{{ paciente.nombre }}</li>
{% empty %}
    <li>No hay ningún paciente registrado aún.</li>
{% endfor %}
</ul>
but if I add:
{% extends 'base.html' %}     # <----- this line

<h1>Pacientes</h1>
<ul>
{% for paciente in object_list %}
    <li>{{ paciente.nombre }}</li>
{% empty %}
    <li>No hay ningún paciente registrado aún.</li>
{% endfor %}
</ul>
I dont see the list anymore!
blob
I want to be able to click each name in the list and see a DetailView of each patient
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 19:17
Ohh I needed the {% block content %} tag!
Interesting
I'll have to read everything again one more time :feelsgood:
Mark Heppner
@mheppner
Nov 15 2016 19:22
@Ismael-VC haha yeah you got it
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 19:24
Is there an easy way to render an html table out of the model data? Initially I planed on doing a tkinter app with pandastable but tk errors are cryptic and that package documentation is not very good
I have pandas installed and I know there is a DataFrame.tohtml method
but I don't see how to use that here, and now the data is in a database, originally I was using plain old csv files
I hope I'm not bothering you guys, please let me know if I do
I want to recreate this application
pantalla_receta_paciente.PNG
ventana_confirmacion.PNG
pantalla_agregar_paciente.PNG
registro_paciente.PNG
pantalla_login.PNG
pantalla_principal.PNG
pantalla_todos_pacientes.PNG
I did this one many years ago at school
but after failing with tkinter, I figured web apps are the way to go! :D
Mark Heppner
@mheppner
Nov 15 2016 19:32
@Ismael-VC I wouldn’t use the DataFrame.tohtml, mostly because you’re already in a web framework. In your view, just load up the data you need, then in the template, make a <table> and use for loops to output each row. Ultimately, you’re going to need some Javascript though to make a decent interface.
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 19:33
Oh god it's so complex :( I didn't wanna touch javascript, I only the one I have copied from other tutorials to make the nav bar
Yes I see looping will have to do it, thanks again!
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 20:02
@mheppner sorry to bother again!
{% extends 'base.html' %}

{% block content %}

<h1>Pacientes</h1>
<ul>
{% for paciente in object_list %}
    <li><a href="{{ object.paciente.get_absolute_url }}">{{ paciente.nombre }}</a></li>
{% empty %}
    <li>No hay ningún paciente registrado aún.</li>
{% endfor %}
</ul>

{% endblock %}
in the above code, how do I get the absolute_url? Currently as is it only returns an empty stirng ""
in the href tag
If I put paciente.get_absoulte_url instead of object.paciente.get_absolute_url I get this error
Reverse for 'paciente_detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
this is my model:
class Paciente(models.Model):
    nombre    = models.CharField(max_length = 50)
    direccion = models.CharField(max_length = 100)
    telefono  = models.CharField(max_length = 20)
    fecha_nacimiento = models.DateField()
    edad      = models.CharField(max_length = 5)
    historial = models.TextField()

    def __str__(self):
        return self.nombre

    @models.permalink
    def get_absolute_url(self):
        return ('paciente_detail', [self.pk])
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 20:27
I think including the same twice is what is causing this, but I cant find an example of what I need:
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r"", include("log_auth.urls")),
    url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}),
    url(r'^logout/$', views.logout, {'next_page': '/login'}),
    url(r"^pacientes/", include("registros.urls"), {'template_name': 'lista_pacientes.html'}),
    url(r"^paciente/", include("registros.urls"), {'template_name': 'paciente_detalle.html'}),    # here I include registros.urls twice
]
Is this what is causing this error?
Blagovest
@bkarakashev
Nov 15 2016 20:29
any body
I am looking for answer for this question
Mark Heppner
@mheppner
Nov 15 2016 21:10
@Ismael-VC in your loop, {{ object.paciente }} isn’t anything, so it silently “errors” out, meaning it just outputs an empty string. {{ paciente }} is your actual model object. you’re almost on the right track in your model, but you should wrap it in reverse() (from django.urls import reverse)
@Ismael-VC docs
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 21:12
@mheppner yes I'm looking at fixing it with reverse, but I can't understand it yet
Mark Heppner
@mheppner
Nov 15 2016 21:12
@bkarakashev you’re in a Django chat room, not YAML or AWS
@Ismael-VC you don’t even have reverse() yet, you’re just returning a tuple.
Ismael Venegas Castelló
@SalchiPapa
Nov 15 2016 21:45
@mheppner thank you so much! I don't know how I missed that doc page, I was only looking at the documentation from the manage.py shell REPL
I think I'm just overloaded by so many concepts for one day!
time to rest
Thanks again, I'm liking this, but it'll take time to sink in!
Have a good day everyone! :D
Mark Heppner
@mheppner
Nov 15 2016 21:47
@Ismael-VC no problem, good luck!