django 调用“listing”函数,即使我告诉它在处理访问 .../watchlist url 时出现的请求时使用“watchlist”函数。我找不到问题。这是错误:
File "/home/simon/Dokumente/cs50WebProgramming/commerce/auctions/views.py", line 103, in listing
listing_obj = AuctionListing.objects.get(id=int(listing_id))
ValueError: invalid literal for int() with base 10: 'watchlist'
Run Code Online (Sandbox Code Playgroud)
网址.py
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("create", views.create_listing, name="create"),
path("after_create", views.after_create, name="after_create"),
path("<str:listing_id>", views.listing, name="listing"),
path("<str:listing_id>/bid", views.after_bid, name="after_bid"),
path("watchlist", views.watchlist, name="watchlist")
]
Run Code Online (Sandbox Code Playgroud)
视图.py
def listing(request, listing_id):
listing_obj = AuctionListing.objects.get(id=int(listing_id))
return render(request, "auctions/listing.html", {
"listing": listing_obj
})
def watchlist(request):
return render(request, "auctions/watchlist.html")
Run Code Online (Sandbox Code Playgroud) 我从nestJS文档(https://docs.nestjs.com/techniques/http-module#http-module)中获取了这个示例,这是我的问题的一个最小示例:
@Injectable()
export class CatsService {
constructor(private httpService: HttpService) {}
findAll(): Observable<AxiosResponse<Cat[]>> {
return this.httpService.get('http://localhost:3000/cats');
}
}
Run Code Online (Sandbox Code Playgroud)
如何从 Observable<AxiosResponse<Cat[]>> 中提取实际的猫数组?我尝试了以下操作,但它给了我一个订阅者对象,我也不知道如何解开该对象以获取实际数据。
const cats = await this.catsService.findAll().subscribe((val) => val);
Run Code Online (Sandbox Code Playgroud) 我通过使用字符串(class )和整数(class )解决了以下leet代码问题https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/。我认为整数解决方案应该更快并且使用更少的内存。但实际上需要更长的时间。当 n=18 和 k=200 时,需要 10.1 秒,而字符串解决方案需要 0.13 秒。SolutionSolution2
import time
class Solution:
def findKthBit(self, n: int, k: int) -> str:
i = 0
Sn = "0"
Sn = self.findR(Sn, i, n)
return Sn[k-1]
def findR(self, Sn, i, n):
if i == n:
return Sn
newSn = self.calcNewSn(Sn, i)
return self.findR(newSn, i+1, n)
def calcNewSn(self, Sn, i):
inverted = ""
for c in Sn:
inverted += "1" if c == "0" else "0"
newSn …Run Code Online (Sandbox Code Playgroud)