Django 1.7: some_name() takes exactly 2 arguments (1 given)

2024/10/7 2:25:52

this is my view.py

from django.http import HttpResponse
import datetime
def current_datetime(request):now = datetime.datetime.now()html = "<html><body>It is now %s.</body></html>" % nowreturn HttpResponse(html)
def hours_ahead(request, offset):offset = int(offset)dt = datetime.datetime.now() + datetime.timedelta(hours=offset)html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)return HttpResponse(html)

this is my urls.py

from django.conf.urls import patterns, url, include
from mysite.view import current_datetime, hours_ahead
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),# url(r'^admin/', include(admin.site.urls)),
(r'^time/$', current_datetime),
#(r'^time/plus/\d{1,2}/$', hours_ahead),
url(r'$', 'mysite.view.hours_ahead', name='hours_ahead'),
)

when i try to go to this localhost:8000/time/plus/24/ I have the error hours_ahead() takes exactly 2 arguments (1 given)

Answer

You need to capture the offset from the url:

url(r'^time/plus/(\d+)/$', 'mysite.view.hours_ahead', name='hours_ahead'),

where (\d+) is a capturing group that would capture one or more digits. In case of localhost:8000/time/plus/24/ it would capture 24.

https://en.xdnf.cn/q/118879.html

Related Q&A

Solving Linear equations with constraint in Python

I have a system of linear equations with some constraints. I would appreciate it if someone could help me solving this system of equations in Python.

systemd service keep giving me error when start or get status

I have a python application and I need it to be run as a service, I tried many methods and I was advised to make it as systemd service I searched and tried some code here is my unit code [Unit] Descrip…

Python 3.5: Print Canvas Text

Could anyone share with me how to print the text of the text widget added to a Canvas object? In the code below, I want the system return the value of "hello" when mouse on the text, however…

What is the most efficient way to match keys from a dictionary to data in text file

Say I have the following dictionary:data=[a 1 : A, b 2 : B, c 3 : C, d 4 : D]and a .txt file which reads:Key a 1 b 2 c 3 d 4 Word as box cow dig(note values are seperated by \t TAB char…

Converting an excel file to a specific Json in python using openpyxl library

I have the Excel data with the format shown in the image preview. How can I convert it into a JSON using Python? Expected Output: file_name = [ { A: Measurement( time=10, X1=1, X2=4 ), B: Measurement(…

Why this algorithm can sort data in descending order

I study python programming and try to sort data in descending order.#sort1 below is successfully sorted but I cannot understand why this happen. Also, data[i], data[data.index(mn)] = data[data.index(m…

Processing.py - Unknown Error on Class Definition

I have no idea how to fix this error. Maybe theres an open parenthesis or quotation mark somewhere before this line?What is wrong with this code?Class Ribbon: # I got an error on this line! def __ini…

How can I implement a stopwatch in my socket Python program

I am creating a very simple ping program in Python that will send 16- 64 bytes of information to a local server, once the server has received all the bytes, it will send back a 1 Byte message back to t…

Making a quiz with shuffled questions

I am wanting to join the Royal Air Force and thought as a good way to prepare I should code myself a quiz about their aircraft.There are 28 aircraft that I have added to the quiz. For example - Where i…

How to create a timer that resets for quiz games?

I trying to create a timer for my quiz game. It should reset after every right question. But problem with my code is that it keeps increasing speed after every time it resets. timeCount = 30 def countd…