How to fix code when changing from django 1.0 to 2.0 [on hold]

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I need help in changing the url.py code in Django 1.0 to Django 2.0

I am a beginner in Django and I am following the book called "The Definitive Guide to Django". The book, however, uses Django 1.0 and I am using Django 2.0, the Django 2.0 syntax for the URLs is however different. and I have tried fixing it but it does not work.

I am fine with suggestions on very good books that utilize Django 2.0 as well. I am still using this book "The Definitive Guide to Django" because I like how it presents things. All suggestions are welcome.

This is my Url.py content

from django.urls import include, path

# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()
#from django.conf.urls.defaults import *
from mysite.views import hello, current_datetime, hours_ahead


urlpatterns = [
    path('hello//', hello, name='hello'),
    #('^time/$', current_datetime),
    #(r'^time/plus/(\d{1,2})/$', hours_ahead)
    # Example:
    # (r'^mysite/', include('mysite.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # (r'^admin/(.*)', admin.site.root),

]

And my views.py file

from django.http import HttpResponse
import datetime
from django.template import Context
from django.template.loader import get_template

def hello(request):
    return HttpResponse("Hello World")


def current_datetime(request):
     now = datetime.datetime.now()
     t = get_template('current_datetime.html')
     c = Context({'current_date':now})
     html = t.render(c)
     return HttpResponse(html)

#def current_datetime(request):
#    now = datetime.datetime.now()
#    return render_to_response('mytemplate.html', {'current_date': now})

def hours_ahead(request, offset):
    try:
        offset = int(offset)
    except ValueError:
        raise Http404()
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    t = get_template('future_time.html')
    c = Context({'offset':offset,
                'dt':dt})
    html = t.render(c)
    return HttpResponse(html)

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.