2017-12-27 316 views
-3

我在python/django中比較新。具有3種型號,例如某些字段:如何從用戶輸入來搜索django/python中的內容?

class Card(models.Model):  
    id = models.AutoField(primary_key=True)  
    cardtype_id = models.CharField(max_length=10) 
    holder_name = models.CharField(max_length=100) 
    card_number = models.IntegerField(default=0) 
    email = models.EmailField(blank=True) 
    birthday = models.DateField(blank=True, default=None) 
    created = models.DateTimeField(default=timezone.now) 
    updated = models.DateTimeField(default=timezone.now) 
    strip = models.CharField(max_length=20, default="strip") 

    def __str__(self): 
     return self.holder_name 

class Transaction(models.Model): 
    id = models.AutoField(primary_key=True) 
    description = models.CharField(max_length=100) 

class CardTransactions(models.Model): 
    card = models.ForeignKey(Card, on_delete=models.CASCADE) 
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE) 
    value = models.DecimalField(max_digits=7, decimal_places=2, blank=True) 
    value_date = models.DateTimeField(default=timezone.now) 
    created = models.DateTimeField(default=timezone.now) 
    description = models.CharField(max_length=200, blank=True) 
    table_value = models.DecimalField(max_digits=7, decimal_places=2, blank=True) 
    discount = models.DecimalField(max_digits=7, decimal_places=2, blank=True) 
    net_value = models.DecimalField(max_digits=7, decimal_places=2, blank=True) 
    doc_number = models.CharField(max_length=20, blank=True) 

我怎麼能要求用戶輸入,例如,「CARD_NUMBER」,並打印出一個HTML頁面上的「說明」?

+1

你有沒有通過Django的教程了:https://docs.djangoproject.com/en/2.0/intro/ tutorial01 /? – dvnguyen

回答

0
from django.forms import model_to_dict 


def my_view(request): 
    card_num = request.GET.get('cc') 
    return HttpResponse(str(model_to_dict(Card.objects.filter(card_number=card_num).first())) 

至少類似的東西

0

你需要寫意見和模板做這個任務。

  • 一個視圖將呈現的HTML模板,你將有一個窗體輸入值。

  • 點擊該按鈕會調用另一個參數爲card_number的視圖,該參數將從與card_number關聯的數據庫中檢索描述,並返回到可根據您的設計顯示某些div的模板。

  • Ajax可用於調用視圖並獲取響應。

見下面的鏈接以供參考:

https://docs.djangoproject.com/en/2.0/intro/tutorial03/

https://docs.djangoproject.com/en/2.0/intro/tutorial04/

相關問題