从htm中获取值的django

2024-06-26 17:43:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用我的crawled tables列出所有表,当我单击table1时,我需要传入表id。有了这个表id我需要得到表名并列出该表中的所有信息。我一直在弄桌子的id

这是我的视图.py

编辑视图.py

def table_base(request):
    table_name = Crawledtables._meta.db_table
    list_tables = Crawledtables.objects.order_by('id')
    return render(request, 'tables/table_base.html', {'table_name': table_name,
                                                      'list_tables': list_tables})


class AboutDetail(DetailView):
    model = Crawledtables
    pk_url_kwarg = 'table_id'
    template_name = 'tables/table_list.html'

    def __init__(self, **kwargs):
        super(AboutDetail, self).__init__(**kwargs)

    def get_object(self, **kwargs):
        if 'table_id' not in self.kwargs:
            return Crawledtables.objects.get(id=1)
        else:
            id_table = Crawledtables.objects.get(id=self.kwargs['table_id'])
            return id_table

# need to create another fuction and i need to pass on the table_id     
# with this table_id i need to get the table_name
# need to use the table_name to get the content using
# Ex: test = AllTables.objects.all()
# i need to return the the content of the table id

这是我的桌子_基本.html

<br>
    <div class="container">
    <div class="jumbotron">
        <h1> {{ table_name }} List</h1>
        {% if list_tables %}
        <table class="table table-bordered sortable">
        <thead>
            <th>Id</th>
            <th>Name</a></th>
            <th>Date</a></th>
            <th>Search Button</th>
        </thead>
            {% for list in list_tables %}
            <tr>
                <td><pre><a href="#" >{{ list.id }}</a></pre></td>


                {# this is where i get the id of the table in CrawledTables and i set it equal to table_id #}

                <td><pre><a href="{% url 'tables:details' table_id=list.id %}" name="table_name">{{ list.name }}</a></pre></td>

                <td><pre>{{ list.date }}</pre></td>`
            </tr>
        {% endfor %}
        </table>
            {% else %}
            <p> No Records Found</p>
        {% endif %}
    </div>
    </div>

这就是我现在的桌子_列表.html

<table class="table table-bordered sortable">
    <thead>
    <tr>
        {{ context }}
        <th>Id</th>
        <th>Title</th>
        <th>Url</th>
    </tr>
    </thead>
        {# make a for loop here #}
            <tr>
                <td> </td>  {# get the id #}
                <td></td> {# get the title #}
                <td><a href="" target="_blank"></a></td> {# get the url #}
            </tr>
    {% endfor %}
</table>

我试着在不使用请求或表单的情况下找出答案…我确实得到了答案…但是我在创建搜索引擎时遇到了困难…所以现在我必须回到使用请求和/或表单的正确方法。。。 请帮助我,我真的不知道我需要把什么命令…和那些django文件没有真正帮助我,因为它更多的关于用户,密码和电子邮件。。。你知道吗

先谢谢你

更新你知道吗型号.py

@python_2_unicode_compatible
class Crawledtables(models.Model):
    name = models.CharField(db_column='Name', unique=True, max_length=100)
    date = models.CharField(db_column='Date', max_length=100)

    class Meta:
        managed = True
        db_table = 'CrawledTables'

    def __str__(self):
        return self.name

    def __unicode__(self):
        return '%name' % {'name': self.name}


@python_2_unicode_compatible
class AllTables(models.Model):
    title = models.TextField(db_column='Title', blank=True, null=True)
    url = models.CharField(db_column='Url', unique=True, max_length=250, blank=True,
                           null=True)
    created_at = models.DateTimeField(db_column='Created_at')

    class Meta:
        managed = False

    def __str__(self):
        return self.url

    def __unicode__(self):
        return self.title

你知道吗表单.py你知道吗

class TableIdForm(forms.Form):
    class Meta:
        fields = ('id', 'name','date')
        model = Crawledtables

class AllTablesForm(forms.Form):
    title = forms.CharField(label='title')
    url = forms.CharField(label='url', max_length=250)
    created_at = forms.DateTimeField(label='Created at')

Tags: thenameselfiddbtablesgetreturn