好的,所以我刚刚在网上完成了一个相当广泛的Django教程,想深入研究我的第一个项目,看看我会如何做。
我刚开始就走了,然后遇到了一个很大的路障,但是我没有运气就试图克服它,所以如果你们能帮助我,我将永远负债累累!
因此,该项目本身只是为我的一些同伴建立了一个网站,我们可以登录并查看我们彼此下注的一些统计信息。
到目前为止我所做的:
我使用以下代码在models.py中创建了两个模型:
from django.db import models
# Create your models here.
class Team(models.Model):
team_name = models.CharField(max_length=500, unique=True)
wins = models.PositiveIntegerField()
losses = models.PositiveIntegerField()
class Predictions(models.Model):
combined_teams = models.CharField(max_length=800)
player_name = models.CharField(max_length=200, primary_key=True)
predicted_wins = models.PositiveIntegerField()
def __str__ (self):
return self.player_name
我创建了一个登录屏幕,这是用户访问的第一个屏幕(与问题无关)
我为几个页面创建了一个具有某些CSS样式的静态文件夹,并对设置文件进行了一些较小的更改。
然后我继续设置我的views.py文件和几个urls.py文件,如下所示:
###VIEWS.PY####
from django.shortcuts import render
from django.views.generic import TemplateView, ListView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Predictions, Team
class WelcomeView(LoginRequiredMixin,TemplateView):
template_name = 'merchbet/welcome.html'
class HomeView(LoginRequiredMixin, TemplateView):
template_name = 'merchbet/home.html'
class PredictionListView(LoginRequiredMixin, ListView):
model = Predictions
def get_queryset(self):
return Predictions.objects.order_by('-player_name')
class GalleryView(LoginRequiredMixin,TemplateView):
template_name = 'merchbet/gallery.html'
from django.contrib import admin
from django.urls import path,include
from django.contrib.auth.urls import views
urlpatterns = [
path('', views.LoginView.as_view(), name='login'),
path('account/profile/', include('merchbet.urls')),
path('admin/', admin.site.urls),
]
from django.urls import path
from . import views
app_name = 'merchbet'
urlpatterns = [
path('', views.WelcomeView.as_view(), name='welcome'),
path('home/', views.HomeView.as_view(), name='home'),
path('predictions/', views.PredictionListView.as_view(), name='prediction_list'),
path('gallery/', views.GalleryView.as_view(), name='gallery')
然后我执行以下脚本,以便可以使用以下程序为本赛季的NBA赌注加载朋友的“预测”:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','mysite.settings')
import django
django.setup()
from merchbet.models import Predictions, Team
predictions = ['Burger', 'Houston Rockets and Oklahoma City Thunder', 101,
'Buzz','Houston Rockets and Boston Celtics', 115, 'Kimon', 'Sacramento Kings and Atlanta Hawks', 44,
'Muller','Utah Jazz and Boston Celtics', 118, 'Barlow', 'Los Angeles Lakers and Milwaukee Bucks', 102,
'Larter','Golden State Warriors and Atlanta Hawks', 83, 'Hume', 'Los Andeles Lakers and Philadelphia 76ers',
104]
def add_predictions():
for index, entry in enumerate(predictions):
if index < len(predictions)-2:
if (type(predictions[index+2]) == type(1)):
player_name = predictions[index]
combined_teams = predictions[index+1]
predicted_wins = predictions[index+2]
preds = Predictions.objects.get_or_create(player_name=player_name,combined_teams=combined_teams,predicted_wins=predicted_wins)[0]
if __name__ == '__main__':
print("Populating the databases...Please Wait")
add_predictions()
print('Populating Complete')
以上方法工作正常,我可以在该站点的Django Admin视图中看到Predictions类的7个对象均以“ player_name”变量命名,因为它是主键:
因此,在所有这些之后,我试图在html文档中简单地使用for循环,这将有助于打印出“ player_name”,然后我可以显示“ combined_teams”和“ predicted_wins”,但我只是不能使它正常工作。
{% for prediction in prediction_list %}
<h1>{{ prediction.player_name }}</h1>
{% endfor %}
我在这篇文章中花了很多力气,所以我希望那里的一些天才能伸出援手!
编辑:我知道文本的颜色是白色,在黑色背景下,所以这不是问题:-)
谢谢!
答案 0 :(得分:0)
对于using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
[Range(1f, 10f)]
public float moveSpeed = 1f;
Touch touch;
private Vector3 touchPosition, whereToMove;
private bool isWalking = false;
float previousDistanceToTouchPos, currentDistanceToTouchPos;
private Animator anim;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
if (isWalking)
currentDistanceToTouchPos = (touchPosition - transform.position).magnitude;
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
previousDistanceToTouchPos = 0;
currentDistanceToTouchPos = 0;
isWalking = true;
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
touchPosition.z = 0;
whereToMove = (touchPosition - transform.position).normalized;
rb.velocity = new Vector3(whereToMove.x * moveSpeed, whereToMove.y * moveSpeed, );
}
}
if (currentDistanceToTouchPos > previousDistanceToTouchPos)
{
isWalking = false;
rb.velocity = Vector2.zero;
}
if (isWalking)
{
previousDistanceToTouchPos = (touchPosition - transform.position).magnitude;
}
float horizontalMovement = Input.GetAxisRaw("Horizontal");
float verticalMovement = Input.GetAxisRaw("Vertical");
// If the player wants to move and is not walking.
// The future position is calculated.
if (horizontalMovement != 0 && !isWalking)
{
whereToMove += Vector3.right * horizontalMovement;
}
else if (verticalMovement != 0 && !isWalking)
{
whereToMove += Vector3.up * verticalMovement;
}
// If the future position is different from the current one, it is because the player wants to move the character.
//if (whereToMove != transform.position)
//{
// transform.position = Vector3.MoveTowards(transform.position, whereToMove, Time.deltaTime * moveSpeed);
// if (!isWalking) { isWalking = true; }
//}
//if (horizontalMovement != 0 || verticalMovement != 0)
//{
//}
// Animating the player when moving
anim.SetFloat("x", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("y", Input.GetAxisRaw("Vertical"));
}
}
,您需要使用object_list
而不是prediction_list
,像这样:
merchbet/prediction_list.html
有关更多详细信息,请检查documentation。