Tuesday, February 28, 2023

Re: How to retrieve the latest object added (new one) in Django models and display it in the template?

Se comments below.


Den fre 24 feb. 2023 kl 12:14 skrev Byansi Samuel <samuelbyansi87@gmail.com>:

Hey everyone, l got a problem. Am trying to retrieve a single latest object added everytime, and display it in the template.
Below is my codes but;

Help me figure it out the source of the problem in these different instances below 👇

_______________________ issue number 1_______

#Action/models.py
class ActionGame(models.Model):
        name=models.Charfield()
        published=models.DateTimeField()

#Action/views.py
from Action.models import ActionGame

def action (request):
        latest_action=ActionGame.objects.filter(published=published). latest()
Context={ 'latest_action': latest_action }
return....

But it returns *error* :
name 'published' is not defined


You haven't defined what the value of published is. If you want to get the last created one the query should be:
latest_action=ActionGame.objects.latest("published") - that would give you the last published item. Another way would be:
latest_action=ActionGame.objects.order_by("published").last()
 

____________________ issue number 2________

#Action/models.py
class ActionGame(models.Model):
        name=models.Charfield()
        published=models.DateTimeField()
        class Meta:
              get_latest_by='published'

#Action/views.py
........
latest_action=ActionGame.objects. latest()
.......

#but it returns *error* :
'ActionGame' object is not iterable

Even if I try this:
............
latest_action=ActionGame.objects. latest('published')
.......

#it returns the Same error:
'ActionGame' object is not iterable

But this second issue, the error is referred in #action.html

{% for x in latest _action %}
<p>{{ x.game_name }}</p>
{% endfor %}


So the latest_action variable that you have sent to the template is an instance of ActionGame - and not a list or anything. You can only access variables in the ActionGame class. For example:

 <p>{{ lastest_action.name }}</p>

would print out the value of the name of the ActionGame instance.

Please l need your assistance.

I'm Samuel,
Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGQoQ3wMUno6hLAO-1FtN0Nn7VtkCn4qf-O4U%3DpeJ4JzY%2B%3DcAQ%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCeBCST7o-5WZx%2BmhGO2yH-Msdse%3DKusk9UOyW8XZBXRBw%40mail.gmail.com.

Re: How can I serve an Angular app from root?

Hi,

You shouldn't be serving your angular application that way. Running it in development, you run it and serve it from the "ng serve" command. Running in production, you need to have a webserver in front of your application any way, and should serve the angular files from the webserver.

Med vänliga hälsningar,

Andréas


Den sön 26 feb. 2023 kl 21:22 skrev Zayd Krunz <zaydkrunz@gmail.com>:
Hello,
I have a prebuilt Angular app, which is basically just static files. How can I serve it from the root project? If I set STATIC_URL to "/", I get the following:

django.core.exceptions.ImproperlyConfigured: runserver can't serve media if MEDIA_URL is within STATIC_URL.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/60f7e50e-7900-46f1-a91e-67038ea91e30n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCdKVjmtThYiKdQnRTYe9PEP-5Smee2LWfqUq7_edLC81Q%40mail.gmail.com.

Monday, February 27, 2023

Re: Getting 4 four latest objects from django model

latest[1] always returns the singular latest object. Use order_by to return multiple objects.

[1] https://docs.djangoproject.com/en/4.1/ref/models/querysets/#latest

On February 27, 2023 2:51:58 AM CST, Byansi Samuel <samuelbyansi87@gmail.com> wrote:

I got a problem with my queryset. Am trying to get 4 latest objects from my model but it returns  1 objects  the top latest.

Below is my code. Any support and guidance is appreciated.

#action/views.py

from  Action.models import ActionGame

latest_action= [ActionGame.objects.filter (os='windows', category='action').latest ('published_date')][:4]

Buy the problem is that it returns 1 object yet l need 4 objects

What  can I do to solve it?

Sunday, February 26, 2023

Re: it is very hard to find both email and phone number as username to login

I use a plugin / python program

AUTHENTICATION_BACKENDS = (
# AxesStandaloneBackend should be the first backend in the
AUTHENTICATION_BACKENDS list.
'axes.backends.AxesModelBackend',

'django.contrib.auth.email-auth.EmailBackend',
'django.contrib.auth.backends.ModelBackend',
)

email-auth.py

place in project root or

/usr/local/lib/python2.7/site-packages/Django-1.11.29-py2.7.egg/django/contrib/auth

use phone number for the user account id and email address will kick in
automatically

can use either for login with valid password



_______________________________________________________________
# cat email-auth.py
from django.contrib.auth.models import User
import re

# vem = Verify E-Mail. This regular expression is from
http://www.regular-expressions.info/email.html
# and is designed to detect anything that meets the RFC-2822 standard.
vem =
re.compile(r"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])""",
re.I)


class BasicBackend:
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

class EmailBackend(BasicBackend):
def authenticate(self, username=None, password=None):
#If username is an email address, then try to pull it up
if vem.search(username):
try:
user = User.objects.get(email=username)
except User.DoesNotExist:
return None
else:
#We have a non-email address username we should
try username
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return None
if user.check_password(password):
return user
____________________________________________________________________________




Happy Sunday !!!
Thanks - paul

Paul Kudla


Scom.ca Internet Services <http://www.scom.ca>
004-1009 Byron Street South
Whitby, Ontario - Canada
L1N 4S3

Toronto 416.642.7266
Main 1.866.411.7266
Fax 1.888.892.7266
Email paul@scom.ca

On 2023-02-26 8:35 a.m., Manobhav Joshi wrote:
> is there any way to use both email and phone number as username to login
> safely which we can use authenticate() function.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/fd5721c4-0a55-48c3-8a1c-5ef75e470e1cn%40googlegroups.com <https://groups.google.com/d/msgid/django-users/fd5721c4-0a55-48c3-8a1c-5ef75e470e1cn%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> This message has been scanned for viruses and
> dangerous content by *MailScanner* <http://www.mailscanner.info/>, and is
> believed to be clean.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a875a175-d6d2-1ab2-c9aa-3460ee1e97c8%40scom.ca.

it is very hard to find both email and phone number as username to login

is there any way to use both email and phone number as username to login safely which we can use authenticate() function.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fd5721c4-0a55-48c3-8a1c-5ef75e470e1cn%40googlegroups.com.

Wednesday, February 15, 2023

Re: I am unable to hash my password.


On Fri, 4 Nov 2022 at 08:01, ritik sahoo <ritiksahoo133@gmail.com> wrote:
your super_staff is not active (blank=True)

On Thu, Nov 3, 2022 at 12:06 PM Saksham Khanal <saksham.khanal01@gmail.com> wrote:
Here you go
On Wednesday, 2 November 2022 at 9:12:54 pm UTC+5:45 hubak...@gmail.com wrote:
Hi  Saksham, please let us see your user model.py and views.py.

On Wed, Nov 2, 2022 at 2:52 PM Saksham Khanal <saksham....@gmail.com> wrote:
when I try to new register new user form the API,the saved user doesn't have it's password hashed which is creating a problem while logging in.You can see the difference between admin and new_user.To create a user model I have inherited a Abstract user class.You can also check my register serializer
Django_group.pngDjango_group1.png

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d9adde23-46cd-40bf-babc-ced3d4b9d9ban%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a358b0a3-b544-4cc5-98fa-394468b662e2n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJwehCtx1z_1FNAfn%2Bh%3DgOsM9BD6EO270Oh8y%3DrPvT0eiH2q-w%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABWmmEBhd2ZMc_gsQ7aDk%3D5%2BAPoYKi2ZNaFMV%2BDhe9d4BQyX8Q%40mail.gmail.com.

Wednesday, February 1, 2023

TabularInlineAdmin queries waaaaaayyyyyy too much.

I have an admin with 1 row in the tabular inline.  I have a custom queryset in

class ExtensionTabularInlineFormSet(BaseInlineFormSet):
    def get_queryset(self) -> QuerySet[Extension]:
        qs = super().get_queryset()

This gets called 20 times to display that one row.  When you have more rows, it gets called 20 times/row.

The culprit is calls to this method:
def initial_form_count(self):
    """Return the number of forms that are required in this FormSet."""
    if not self.is_bound:
        return len(self.get_queryset())
    return super().initial_form_count()

The solution would be to cache this value, but admin views seem like singletons way too often for this to work.  Anyone else seen this?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f4bacbd9-b519-45af-8e5b-826c4b5d9c88n%40googlegroups.com.

Re: Adding Microsoft Word Editor to Django

There are some alternatives that I can think of.


Etherpad is a web editor that has a plugin for import docx documents

If you can use php:

If you can use odf instead of doc:

This is just my 2 cents, and I have never used these before.
I hope this helps.


Thiago


Em qua., 1 de fev. de 2023 às 08:51, o1bigtenor <o1bigtenor@gmail.com> escreveu:
On Wed, Feb 1, 2023 at 2:16 AM Shittu Abdulrasheed
<rasheedshittushittu@gmail.com> wrote:
>
> Good day my co django-developers
>
> I have a project going on for my client and he want a features where users will be able to upload a word documents file and he/she will be able to edit the documents in our app just like editing in msword and save it back to our database.
> How can i acheive that?
> I have try Many WYSIWYG but it is just only displaying the html format without the sytles included eg. Ckeditor,Tinymce.
>
>
Methinks that you get to write a WYSIWYG text editor for that client.
Might be easier for the doc owner to do their editing on their own
software and then uploading the final doc.
If that needs editing - - - well then someone downloads the doc shifts
it back to a word processor file from pdf
does the editing changes back to a pdf and then uploads the now edited
file. That process is fraught with
possible gotchas - - - I can see why you might want such an
application - - - don't know of one though.

Maybe someone else has a better idea?

HTH

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPpdf592ad7Yx_8CMcaRLBBW258%3DUmyjsG5U3D6Z8htreBn3ig%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANbmKyuLycDv%2BgL5YaypnT3cvDWRdCZxi_ihP-zax7WdMC0y1A%40mail.gmail.com.

database design approach

I am designing a database for a django project. I already read this tutorial that I found really useful: https://learndjango.com/tutorials/database-design-tutorial-beginners

I have a question regarding the normalization. The normalization is important due that we need to reduce redundant data. But we need to find a balance because increasing the complexity is decreasing the performance.

I have a model (Player) that refers to the User Model.  I need to add several fields in the Player model. Is it better to put every field in the Player model or I can use three levels of complexity?

Approach 1: User Model > Player Model (with many fields)
Approach 2: User Model > Player Model > Several Different Models for each different features.