简体   繁体   English

如何让用户输入以在 django/python 中搜索内容?

[英]How do I make a input from the user to search for something in django/python?

I'm relatively new in python/django thing.我在 python/django 方面相对较新。 Having 3 models with some fields for example:例如,有 3 个带有某些字段的模型:

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)

How can I ask the user to input, for example, the "card_number" and print out the "description" on a HTML page?我如何要求用户输入,例如,“card_number”并在 HTML 页面上打印出“描述”?

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()))

at least something like that至少是这样的

You will need to write views and templates to do this task.您将需要编写视图和模板来完成此任务。

  • One view would be to render the html template where you will have a form to input the value.一种视图是呈现 html 模板,您将在其中使用表单来输入值。

  • Clicking on the button will call another view with parameter card_number which will retrieve the description from the database associated with the card_number and return back to the template where the same can be shown with some div as per your design.单击该按钮将调用另一个带有参数 card_number 的视图,该视图将从与 card_number 关联的数据库中检索描述并返回到模板,其中可以根据您的设计使用一些 div 显示相同的内容。

  • Ajax can be used to call the view and fetch the response. Ajax 可用于调用视图并获取响应。

See below links for reference:请参阅以下链接以供参考:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM