Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9221583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:36:36+00:00 2026-06-18T03:36:36+00:00

I am trying to code without throwing any warnings in my console. So far

  • 0

I am trying to code without throwing any warnings in my console. So far I have been pretty good at avoiding it until this one case, which seems like a chicken and egg situation to me.

from datetime import datetime as dt 

last_contacted = "19/01/2013"
current_tz = timezone.get_current_timezone()
date_time = dt.strptime(last_contacted, get_current_date_input_format(request))
date_time = current_tz.localize(date_time)

The third line is throwing this warning:

RuntimeWarning: DateTimeField received a naive datetime (2013-01-19
00:00:00) while time zone support is active.)

Its kind of odd, since I need to convert the unicode into a datetime first before I can convert the datetime object into an datetime-aware object (with timezone support) in the forth line.

Any suggestions from experts?

Thanks

UPDATE:

def get_current_date_input_format(request):
    if request.LANGUAGE_CODE == 'en-gb':
        return formats_en_GB.DATE_INPUT_FORMATS[0]
    elif request.LANGUAGE_CODE == 'en':        
        return formats_en.DATE_INPUT_FORMATS[0]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:36:37+00:00Added an answer on June 18, 2026 at 3:36 am

    From the comments to your question I am guessing that what you really have in your code is something like this:

    from datetime import datetime as dt 
    
    last_contacted = "19/01/2013"
    current_tz = timezone.get_current_timezone()
    model_instance.date_time = dt.strptime(last_contacted, get_current_date_input_format(request))
    model_instance.date_time = current_tz.localize(date_time)
    

    where model_instance is an instance of a Model which has a DateTimeField named date_time.

    class MyModel(models.Model)
        ....
        date_time = DateTimeField()
    

    The Python datetime.strptime function returns a naive datetime object which you are attempting to assign to the DateTimeField which is then generating a warning because the use of non-naive datetime objects is incorrect when timezone support is enabled.

    If you combine the calls to strptime and localize on a single line, then the complete calculation of converting from a naive datetime to non-naive datetime is done before assigning to date_time and so you won’t get an error in this case.

    Additional note: Your get_current_date_input_format function should return some default timezone to use in the event that there is no timezone in the request, otherwise the strptime call will fail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two pieces of code that are messing up without throwing any
I am trying to parse this Django code (without any familiarity with Django, I
Im trying to get debugging working without an app.config. I have the following code:
I was trying out the following code and its repeatedly throwing the 'else without
I was trying the next code without success HTML <a id=addBookButton href=javascript:showForm('addBookButton','add-book','activateAddBookForm'); class=addA><span>Add Book</span></a>
I'm trying to code a page where you can post a comment without reloading
I'm trying to programmatically add dots to a radar screen. The code runs without
I am having an issue converting type. I was trying code like this (minimal,
I´m trying to code a tooltip (Yes I know, I have my reasons to
I am trying to code AES using Cipher Block Chaining(CBC) mode. I am pretty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.