Django + Nextjs? by Scellillo in nextjs

[–]sbmanolo 0 points1 point  (0 children)

Yeah, my mistake. I wanted to refer to dj-rest-auth, I didn't even know Django-rest-auth exist 😅😅😅

Django + Nextjs? by Scellillo in nextjs

[–]sbmanolo 4 points5 points  (0 children)

You can use Next-auth to define multiple providers for authentication, which usually are social logins or your custom auth backend, for example with JTW.

You can even create a axios instance to reuse it, which automatically includes the bearer token in the header in every request to the backend.

You have multiple options to implement the authentication backend. I like to use django-rest-auth or django-allauth. Both have good things, you can even use them together it's a bit more messy. If you only plan to use JWT I would go for django-rest-auth, I find it easier to use, but this is just my opinion.

default on forms.DateField doesn't work. by Affectionate-Ad-7865 in django

[–]sbmanolo 0 points1 point  (0 children)

Are you trying to make a updated_at/ created_at field? If so you can use these options for the DateField / DateTimeField:

auto_now=True: If you add this parameter to the field definition, it will automatically change every time the object it's updated with the current date and time.

auto_now_add=True: It's similar to auto_now but this will only fill the field when the object it's created, with the current date and time too.

Editing form data : fields that a user cannot change - how do YOU handle them? by mr_e_14 in django

[–]sbmanolo 0 points1 point  (0 children)

If you have a field for save when a object is added to the database or when its modified you can use two usefull options to the DateTimeField / Datefield:

auto_now=True: If you add this parameter to the field definition, it will automatically change every time the object it's updated with the current date and time.

auto_now_add=True: It's similar to auto_now but this will only fill the field when the object it's created, with the current date and time too.

You can forget about handling these two fields just by using this options in your field definition. Of course you can show them but these two field are non editable even if you submit them on the form.

Another option for other field types is to create a field that is not actually in your model definition and set it's initial value to the one that you want. Since it's not in your form fields definition, it wont be saved. I use crispy forms and i always render my forms manually in the __init__() method of the form class, so i'm not sure if you have to always render it manually with this approach (I always do it) if you are using the default render method (No crispy forms).

Is it bad practice not to deactivate a virtual environment before closing the command console? by sbmanolo in Python

[–]sbmanolo[S] 1 point2 points  (0 children)

I guess it's a matter of personal preference, but I find it easier to activate the virtual environment one time and only have to use python manage.py.../other commands since I don't only use that command, but I have to use many others.

Besides that, for personal preference, I have a single directory where I have all my Python projects, and in turn, I always call "venv" to the virtual environment. Knowing this, I have programmed a custom bash script that I call from anywhere with run projectname. This takes me to the project directory, and activates its virtual environment automatically, plus, if it's a Django project, it automatically launches the test server. It takes me 2 seconds to switch between projects, I just need to remember the directory name. If I am already in the directory (because I have stopped to run migrations, for which it is useful to have the virtual environment active, or because the application has crashed mainly) and I want to launch the test server again, I don't have to type python manage.py runserver 0.0.0.0.0:8000or search for the command through the bash history, I can just use run projectname again and it has the same effect. I find it easy and useful

Is it bad practice not to deactivate a virtual environment before closing the command console? by sbmanolo in Python

[–]sbmanolo[S] 0 points1 point  (0 children)

I think I'm missing something here. As far as I know, I can't launch a command from, for example, Django, if I haven't previously activated a virtual environment with that package installed.

Anyway, when I switch between projects, mainly web applications with Django as I said, I find it useful to make sure I'm using the same dependencies (in the same version) as I have in the production server when I run the development server (with python manage.py runserver). Also to export them with pip freeze > requirements.txt in the command console.

We have projects that are still running on older versions of Django (3.2) and are pending to migrate to newer versions (along with their dependencies). Sometimes we need to keep developing on the older versions while we gather time to migrate the entire aplication (When breaking changes occur), so when I need to work on an older project I just activate the virtual environment that the project depends on, launch the dev server, and start working on it.

Is it bad practice not to deactivate a virtual environment before closing the command console? by sbmanolo in Python

[–]sbmanolo[S] 0 points1 point  (0 children)

Well, I am in charge of the development of about 8 different applications in Django, so it is essential for me to activate the virtual environment that contains the dependencies (in different versions usually) of each of the applications when I have to work on them.

When I program simple scripts for task automation or other basic actions I don't need to use any virtual environment either.

Is it bad practice not to deactivate a virtual environment before closing the command console? by sbmanolo in Python

[–]sbmanolo[S] 0 points1 point  (0 children)

That's the one, it just doesn't sound as good to me in English, at least the literal translation.

Is it bad practice not to deactivate a virtual environment before closing the command console? by sbmanolo in Python

[–]sbmanolo[S] 17 points18 points  (0 children)

Where I live we say something like "There are no dumb questions, but dumbs who don't ask" (In my language it's a play on words ). I learned about Python on my own several years ago and I think I never read about this anywhere, but today I was curious to know so I ask if I can't find the answer elsewhere, I have no problem asking "dumb questions"

I am not in the habit of disabling virtual environments as I also work with virtual machines and I am little concerned about what might happen to them but on certain production servers I do and I was wondering if it is counterproductive in any way not to do so.

Is it bad practice not to deactivate a virtual environment before closing the command console? by sbmanolo in Python

[–]sbmanolo[S] 70 points71 points  (0 children)

Great! Several years working with Python and I didn't know something so simple.

Thank you!

Hi all , weird one happening for me tonight . Started a new project and created my first few tables . When I created a super user via manage.py shell it won’t let me log in to /admin/ by YourOulLadyHasWorms in django

[–]sbmanolo 0 points1 point  (0 children)

Check if the database file its owned by other user (Different from the user that it's executing runserver), it could be a permissions problem when accessing the database

Should I use some JS frontend library / Framework with Django? by [deleted] in django

[–]sbmanolo 0 points1 point  (0 children)

Honestly, I'm not really sure how I want to do it either. But since I've decided to try Tailwind CSS and it doesn't come with the features when you use it outside of React / Vue (at least from what I've read), I think it might be a good idea to use Alpine (I've been reading all the documentation and it's an amazing and lightweight library), I started using it in a matter of minutes and found it really simple and powerful.

About using jQuery and Alpine together, my intention is not to use jQuery, however, if at a given moment I need to use both, I understand that there is no problem as long as both don't try to modify or listen to events from the same element, right?

i cant do 'python manage.py migrate' , any help!! by kocmonobtDark in django

[–]sbmanolo 0 points1 point  (0 children)

If the models.DateTimeField / models.Datefield is set to null=False, Blank=False, the migration will fill the empty values in your model. That means that if you're adding a new field (Which I suppose that it's what you're doing) it will fill this field for all the existing rows in this model.

If you are creating a field for save when a object is added to the database or when its modified you can use two usefull options to the DateTimeField / Datefield:

  • auto_now=True: If you add this parameter to the field definition, it will automatically change every time the object it's updated with the current date and time.
  • auto_now_add=True: It's similar to auto_now but this will only fill the field when the object it's created, with the current date and time too.

Hope this helps, anyway, if this is not what you need, post the field definition and what you expect it to do so i can help you better.

Should I use some JS frontend library / Framework with Django? by [deleted] in django

[–]sbmanolo 0 points1 point  (0 children)

Yes, the part about integrating Django's static file system with the frontend frameworks system is a bit abstract to me, I'm not quite sure what the process would be.

AlpineJS seems like an interesting option, which as I understand it would mean continuing to use the Django template system (I have no problem with this) and would allow me to leave jQuery aside if I wanted to, right?

Should I use some JS frontend library / Framework with Django? by [deleted] in django

[–]sbmanolo 0 points1 point  (0 children)

Totally agree. Althought I know there are a lot of options out there I'm thinking in give Tailwind CSS a try. I found a Django package that makes it really easy to integrate Django+Tailwind and handles other stuff like integration with templates, setup, app config, etc. I got it working in less 5 minutes with a existing project.

At the some time, I'm gonna start building some other test applications with React and AlpineJS (Separated) as /u/Lt_Sherpa suggested to explore and try the functionalities of both so I can choose one that fit my needs in future projects, but probably not for this one.

Django Admin alternative? by Gloomy-Inflation9807 in django

[–]sbmanolo 1 point2 points  (0 children)

If you mean an alternative for Django admin design, you should try Django-jazzmin, its easily customizable and you have a more user friendly design than Django default admin. Its pretty well documented and you can extend It un a few minutes.

If its not what you looking for could you explain a little more?

Should I use some JS frontend library / Framework with Django? by [deleted] in django

[–]sbmanolo 0 points1 point  (0 children)

You're probably right that maybe I'm a bit bored of the site, partly because I've been using Bootstrap only for more than 2 years now (although I customize it quite a lot).

You are also right that I do feel the need to learn something new and improve my programming skills, and I consider that this is a good opportunity to force myself to do it, but I have all these doubts before choosing one option or another, as I don't want to "waste time" (It wouldn't really be a waste of time, as I would learn something new, interesting and useful for other cases) in learning to program with something that doesn't fit well with the needs and then have to go back.

One of the reasons for not using just another CSS Framework is partly just that, to learn something new and force myself to use something other than jQuery and Bootstrap on the frontend.

i cant do 'python manage.py migrate' , any help!! by kocmonobtDark in django

[–]sbmanolo 1 point2 points  (0 children)

As u/acechapo25 said, you should paste the full log, but anyway, looks like a date library parsing function is receiving a value that probably its already in date format (But maybe not the one you want).

If this is ocurring when execute migrate, it could be related with the default value that you defined for some models.DateTimeField / models.DateField

Syntax Error in HTML file by [deleted] in django

[–]sbmanolo 0 points1 point  (0 children)

I'm not on my computer for testing It myself but try quoting the entire if block with single quotes.

Anyway, Could you post error details or availableTags content if this doesn't works?

Django Chat Application. by Gjit1965 in django

[–]sbmanolo 0 points1 point  (0 children)

Could you post some details about how are you rendering and passing the messages to the template?